Multiple TextWatchers to single EditText in android

229 Views Asked by At

when I tested adding two TextWatchers to one EditText, it seems like first TextWatcher that is registered gets ignored and only TextWatcher that is registered last works fine. for example,

myEditText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(...) {}
            override fun onTextChanged(...) {}
            override fun afterTextChanged(s: Editable) { // first logic here}
        })

then I add one more TextWatcher to myEditText

myEditText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(...) {}
            override fun onTextChanged(...) {}
            override fun afterTextChanged(s: Editable) { // second logic here}
        })

now first logic gets ignored and only second logic remains and works.
I wonder if there is a way to make both of them work together.

(if someone who has experience of hbb20 ccp library, could you also take a look at this thread and get me some help as well?)
I appreciate your help in advanced.

Edit:
my real problem was at logic1 and logic2 not on two TextWatchers. logic1 and logic2 were trying to update one same variable with two different conditions. my code was something like this(which was the problem).

button1.isEnable = s.toString().length == 2  // in first TextWatcher
button1.isEnable = s.toString().length == 3  // in second TextWatcher

so actually second TextWatcher's condition was overriding first TextWatcher's condition.

Conclusion: multiple TextWatchers on single EditText will work just fine

my questions was wrong since my logic was wrong. but thanks to all who tried to help me!

1

There are 1 best solutions below

0
aref behboodi On
public interface MyTextWatcher {
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after);

    public void onTextChanged(CharSequence s, int start, int before, int count);

    public void afterTextChanged(Editable s);
}

Now you can use multiple TextWatcher such as below :

public class TextWatcherActivity {

    EditText editText;
    List<MyTextWatcher> myTextWatcher = new ArrayList<>();


    public void init() {

        myTextWatcher.add(new MyTextWatcher() {
            @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) {

            }
        });

        myTextWatcher.add(new MyTextWatcher() {
            @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) {

            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                for (MyTextWatcher textWatcher : myTextWatcher) {
                    textWatcher.beforeTextChanged(s, start, count, after);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                for (MyTextWatcher textWatcher : myTextWatcher) {
                    textWatcher.onTextChanged(s, start, before, count);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                for (MyTextWatcher textWatcher : myTextWatcher) {
                    textWatcher.afterTextChanged(s);
                }
            }
        });
    }
}