SameJall
SameJall

Reputation: 113

Else if in the textWatcher

I am a beginner in android programming & I am working on my first App, I have 4 editText boxes & I need to make a relation between the first editText Box "air flow" & the second box "frictionLoss" then the result must be stored in the last box without clickListner ... then I need to make a relation between the first & the third Box "Velocity"also the result to be updated in the 4th Box, so I made a textWatcher & I added else if statement, the main issue is the first if statement is working perfectly, but whenever I tried to change the value for the third box it won't work till I remove the entry manually in the App from the second box then the result will be shown.


import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RectangularDuctActivity extends AppCompatActivity {
    public EditText airFlow;
    EditText frectionLoss;
    EditText velocity;
    TextView result;
    Button mbutton;
    EditText equivalentDia;
    EditText    recWidthMm;
    EditText    recHeightMm;
    int airFlowCfm = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rectangular_duct);

        airFlow = findViewById(R.id.air_Flow_View);
        frectionLoss = findViewById(R.id.press_loss_View);
        equivalentDia = findViewById(R.id.equal_Dia_View);
        velocity = findViewById(R.id.velocity_View);
        result = findViewById(R.id.result_textview);
        mbutton = findViewById(R.id.cal_button);
        recWidthMm = findViewById(R.id.width_View);
        recHeightMm = findViewById(R.id.height_View);

        //text watcher is added to allow the cells to be updated once the related cells are edited
        // this watcher is controlling the Equivalent Dia & the velocity once Air flow & friction loss are editied
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                double deFinal = 0;
                double a = Integer.parseInt(airFlow.getText().toString());
                if (!airFlow.getText().toString().equals("") && !frectionLoss.getText().toString().equals("")) {
                    //double a = Integer.parseInt(airFlow.getText().toString());
                    double b = Double.parseDouble(frectionLoss.getText().toString());
                    double de = (Math.pow(((0.109136 * Math.pow((a * 2.119), 1.9)) / (b * 12 / 98.1)), (1 / 5.02)) * 25.4);
                    deFinal = deFinal + de;
                    equivalentDia.setText(String.valueOf(deFinal));
                    velocity.setText(String.format("%.2f", (((a / 1000) / (Math.PI * (Math.pow((de / 2000), 2)))))));

                } else if (!airFlow.getText().toString().equals("") && !velocity.getText().toString().equals("")) {
                    //double a1 = Integer.parseInt(airFlow.getText().toString());
                    double b1 = Double.parseDouble(velocity.getText().toString());
                    double de2 = (a*10);
                    deFinal = deFinal + de2;
                    equivalentDia.setText(String.valueOf(deFinal));
                    //equivalentDia.setText(String.valueOf(Math.round(deFinal)));
                    //frectionLoss.setText(String.valueOf(Math.round(b1)));

                }else {
                    equivalentDia.getText().clear();
                    //velocity.getText().clear();
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        frectionLoss.addTextChangedListener(textWatcher);
        airFlow.addTextChangedListener(textWatcher);'''

Upvotes: 0

Views: 59

Answers (1)

SameJall
SameJall

Reputation: 113

Yesterday after a break, I adjusted the textWatcher code & I totally removed the the else if statment as per the below code

public void onTextChanged(CharSequence s, int start, int before, int count) {
            double deFinal = 0;
            if (!airFlow.getText().toString().equals("") && !frectionLoss.getText().toString().equals("")) {
                double a = Integer.parseInt(airFlow.getText().toString());
                double b = Double.parseDouble(frectionLoss.getText().toString());
                double de = (Math.pow(((0.109136 * Math.pow((a * 2.119), 1.9)) / (b * 12 / 98.1)), (1 / 5.02)) * 25.4);
                deFinal = deFinal + de;
                equivalentDia.setText(String.valueOf(deFinal));
                velocity.setText(String.format("%.2f", (((a / 1000) / (Math.PI * (Math.pow((de / 2000), 2)))))));
            }else {
                equivalentDia.getText().clear();
                velocity.getText().clear();
                frectionLoss.getText().clear();
            }

        }

then after that I added setOnClickListener on the touch of the third button as per the below code

velocity.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
            double b1 = Double.parseDouble(velocity.getText().toString());
            double a1 = Integer.parseInt(airFlow.getText().toString());
            frectionLoss.getText().clear();
            if (!airFlow.getText().toString().equals("") && !velocity.getText().toString().equals("")) {
                double areaV = (a1/1000)/b1;
                double de2 = (Math.pow((areaV*4)/Math.PI,0.5)*1000);
                double deltaH = 0.14939257 * (b1 / (Math.pow((de2/1000),3.02)));
                velocity.setText(String.format("%.2f", b1));
                equivalentDia.setText(String.valueOf(de2));
                frectionLoss.setText(String.format("%.2f", deltaH));

            } else {
                equivalentDia.getText().clear();
            }

        }
   });

Now the app is working as I wanted

Upvotes: 1

Related Questions