SameJall
SameJall

Reputation: 113

3 editText & textWatcher problem in android

Dear All I need help in this issue, I am working on a small App to insert it as a part of anther App, in this App there are 3 EditText number1, number2, & number3.. i need to make the summation of editText number 1 & 2 to be reflected in number3 editText without clicking button, also in the same time if number1 & number3 values available number2 will be the sum (without button)... I made if statement to send the editText to the textWatcher, the App is running perfectly for the numbers below 10, once the numbers increased to more than 10, the App will take the second editTextvalue as integer with one digit only (in this case the 10th number only) enter image description here

please find the JAVA code

    private EditText number1;
    private EditText number2;
    private EditText number3;
    Button reset;

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

        number1=findViewById(R.id.edit_text_1);
        number2=findViewById(R.id.edit_text_2);
        number3=findViewById(R.id.edit_text_3);
        reset=findViewById(R.id.reset);

//=========== to reset the editText
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String dx = number2.getText().toString().trim();
                number1.getText().clear();
                number2.getText().clear();
                number3.getText().clear();
            }
        });

//======== if statement to add Edit Text 1 & 2 to text watcher
        if(number1.getText().toString().equals("") && number2.getText().toString().equals("")){
            number1.addTextChangedListener(loginTextWatcher);
            number2.addTextChangedListener(loginTextWatcher);
        }
//======== if statement to add Edit Text 1 & 3 to text watcher 2
        if(number1.getText().toString().equals("") && number3.getText().toString().equals("")){
                number1.addTextChangedListener(loginTextWatcher2 );
                number3.addTextChangedListener(loginTextWatcher2 );
        }


    }

//=========== TextWatcher when EditText 1 & 2 are used & EditText 3 is empty...
// ==========the value of EditText3 will be the sum of EditText 1 and 2
    private TextWatcher loginTextWatcher  = 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) {
            if(!number1.getText().toString().equals("") && !number2.getText().toString().equals("")){

                if(number3.getText().toString().equals("")){
                    Double value1 = Double.parseDouble(number1.getText().toString().trim());
                    Double value2 = Double.parseDouble(number2.getText().toString().trim());
                    number3.setText(String.valueOf(value1+value2));
                }

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

//=========== TextWatcher when EditText 1 & 3 are used & EditText 2 is empty...
// ==========the value of EditText2 will be the sum of EditText 1 and 3
    private TextWatcher loginTextWatcher2  = 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) {
            if(!number1.getText().toString().equals("") && !number3.getText().toString().equals("")){

                if(number2.getText().toString().equals("")){
                    Double value1 = Double.parseDouble(number1.getText().toString());
                    Double value3 = Double.parseDouble(number3.getText().toString());
                    number2.setText(String.valueOf(value1+value3));
                }

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

}

please somebody advise

Upvotes: 0

Views: 310

Answers (1)

SameJall
SameJall

Reputation: 113

First of all Thanks for everybody helped in this, I finally find a good solution & the App is working as I wanted 1- I removed the unnecessary if statement as advised by Shay Kin 2- I added editText1 since it is common between all the calculation methods to the watchListener the two of them. 3- I removed the if statement in the onCreate method & replace them with setOnFocusChangeListener for editText2 & 3... in this I have to remove the opposite editText from its textWatcher to avoid freezing in the App. the code i added is

//=========== this is to add editText 1 to the textWatchers for the two condition by default
        number1.addTextChangedListener(loginTextWatcher);
        number1.addTextChangedListener(loginTextWatcher2);
//=========== this ti set the focus of editText 2 and send it to the related textWatcher
        number2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    // must remove editText 3 to from its textWather to avoid App freezing
                    number3.removeTextChangedListener(loginTextWatcher2);
                    number2.addTextChangedListener(loginTextWatcher);

                }

            }
        });
//=========== this ti set the focus of editText 3 and send it to the related textWatcher
        number3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    // must remove editText 2 to from its textWather to avoid App freezing
                    number2.removeTextChangedListener(loginTextWatcher);
                    number3.addTextChangedListener(loginTextWatcher2 );
                }
            }
        });

also in the textWatcher the if condition should be adjusted from

if(!number1.getText().toString().equals("") && !number3.getText().toString().equals("")){

to

if(!number1.getText().toString().equals("") && number3.hasFocus()){

because if you kept the first condition and used the textWatcher for editText 1 & 2 twice (or several times) & then you used the arrangement 1 & 3 the App will freeze.

Upvotes: 1

Related Questions