Will Will - 10 months ago 69
Android Question

Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

Under what circumstances should I use

instead of
and vice versa? Examples would be most instructive with attention to why
must be Overridden but
do not have to be Overridden.


These events are called in the following order:

  1. beforeTextChanged(CharSequence s, int start, int count, int after).
    This means that the characters are about to be replaced with some new text. The text is uneditable.
    Use: when you need to take a look at the old text which is about to change.

  2. onTextChanged(CharSequence s, int start, int before, int count).
    Changes have been made, some characters have just been replaced. The text is uneditable.
    Use: when you need to see which characters in the text are new.

  3. afterTextChanged(Editable s).
    The same as above, except now the text is editable.
    Use: when you need to see and possibly edit the new text.

If I'm just listening for changes in EditText, I won't need to use the first two methods at all. I will just receive new values in the third method and correct new text if needed. However, if I had to track down exact changes which happen to the values, I would use the first two methods. If I also had a need to edit the text after listening to the changes, I would do that in the third method.