Search code examples
javaandroidevent-handlinginfinite-loop

How can I change the EditText text without triggering the Text Watcher?


I have an EditText field with a Customer Text Watcher on it. In a piece of code I need to change the value in the EditText which I do using .setText("whatever").

The problem is as soon as I make that change the afterTextChanged method gets called which created an infinite loop. How can I change the text without it triggering afterTextChanged?

I need the text in the afterTextChanged method so don't suggest removing the TextWatcher.


Solution

  • You could unregister the watcher, and then re-register it.

    Alternatively, you could set a flag so that your watcher knows when you have just changed the text yourself (and therefore should ignore it).