Daniel Daniel - 9 months ago 58
Android Question

Why TextWatcher needed

So, after going through some docs regarding TextWatcher, I'm still not too sure why it's actually needed. Any help with real example (no code) will be great. Thanks.

Answer Source

TextWatcher is needed to know if a field text has been changed.

Presume you have a mobile number, and you need to check the length of it. As a standard mobile number should contain 10 digits. You have an error field at the side which will go away once the edittext length will be greater than 10. How will you know that now the length is greater than 10?

To overcome this we have textwatcher method which watch over the edittext field. Generally we will check length of edittext in AfterTextChanged callback and it will work in realtime. After that you can make your logics behind what to do after the length is greater than 10.

This is just an example. There are certain many more ways in which you can use textwatcher.