ankit yadav
ankit yadav

Reputation: 1

How to disable textwWatcher when I use EditText.setText()?

I am working in an android studio project where I am trying to make a number system convertor app. I have used 2 EditTexts (Binary and Decimal). APP SCREENSHOT

Here I want that whenever user give a input to decimal EditText, its corresponding value should be set in the binary EditText and vice-versa. I have used TextWatcher in each of the EdtTexts to calculate whenever user changes the value in the input fields. But I am having an issue that when the app crashes whenever something is input to any of the EditTexts.

TextWatcher used for decimal EditText:-

TextWatcher textDec=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) {


        }

        @Override
        public void afterTextChanged(Editable s) {
            String binValue;
            String decString;
            decString = dec.getText().toString();
            if (decString.trim().equals("")) {
                binValue = "";
            }
            else {
                binValue = decToBinMain(decString);
            }
            if (bin.getText().toString()!=binValue) {
                textView4.setText(binValue);
                bin.setText(binValue);
            }
        }
    };

    dec.addTextChangedListener(textDec);

Textwatcher used for binary EditText:-

TextWatcher textBin=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) {
        String binValue;
        String decString;
        binValue=bin.getText().toString();
        if(binValue.trim().equals("")){
            decString="";
        }
        else {
            decString = String.valueOf(binToDec(binValue));
        }
        if(!dec.getText().toString().equals(decString)) {
            textView.setText(decString);
            dec.setText(decString);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};


bin.addTextChangedListener(textBin);

I have no Idea what is the issue and why the app crashes on input to any of the field. However, when I set the corresponding values to a textView, it does work perfectly. Any help would be aprpreciated!! ThankYou!!

Upvotes: 0

Views: 490

Answers (2)

AndroidEngineX
AndroidEngineX

Reputation: 1461

You can see my answer at SO. Just to give you the gist of the solution. Set some flag(this mechanism can vary. You can also see different ways people has come up with to solve the issue). At your core logic see if that flag is set and if yes don't run that core logic.

Please see my other approaches at the SO

Upvotes: 0

Yavor Mitev
Yavor Mitev

Reputation: 1508

You have a circular call:

dec -> bin -> dec -> bin -> dec -> bin

  1. I would advise you to extract some code in methods to make the code more readable for your own sake.

  2. After you do it you might have a better idea than mine. And mine is with a flag:

     // in the Activity
    

    boolean update = true;

    // Put this in each TextWatcher

             if(update) {
                 // execute your code here
             };
    
             update = !update;
    

Upvotes: 0

Related Questions