Android: Applying Text Watcher on an EditText

Updated 14 December 2016

Save

In android you can apply TextWatcher on an EditText and perform action based on changes in the EditText. It is particularly useful when you want to perform some action while the user is entering text like auto save feature of text or suggestion feature. So in that case we can apply a TextWatcher and attach the methods we want to perform on text change.

There are three methods of TextWatcher we can use based on our need.

a. beforeTextChanged( CharSequence s, int start, int count, int after )

This method is used to notify you that in the CharSequence s, count number of character that start at start are about to be changed with a new text having after number of characters.

b. onTextChanged( CharSequence s, int start, int before, int count)

It is just the reverse of earlier one. This method is used to notify you that in the CharSequence s, count number of character that start at start are already been changed with older text having before number of characters.

c. afterTextChanged(Editable s)

It is the mostly used method as it performs after the text is changed and we generally have to perform something when the text is changes like saving it for the user. Some people perform text changes in this method and if you do so be careful that you don’t end up in an infinite loop as after any change to the text this method is again called.

I had used it to save the value of the text so that the user always get the updated value and loss of the data can be prevented. Below is the dummy code:

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home