Monitor the Changes in EditText

There is no “setOnXXXXXXXListener” Method in Android for EditText. If we want to monitor the changes in EditText, how can we achieve that ?

In fact, it’s simple because of the Method — addTextChangedListner(TextWatcher watcher)

In TextView|Android Developers, we can find a Method called addTextChangedListener(TextWatcher watcher).:

public void addTextChangedListener (TextWatcher watcher)

Added in API level 1

Adds a TextWatcher to the list of those whose methods are called whenever this TextView’s text changes.

About TextWatcher, it has three Public Method:

abstract void afterTextChanged(Editable s)

This method is called to notify you that, somewhere within s, the text has been changed.

abstract void beforeTextChanged(CharSequence s, int start, int count, int after)

This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with lengthafter.

abstract void onTextChanged(CharSequence s, int start, int before, int count)

This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had lengthbefore.

So you can use it to monitor your changes in EditText just like this:

editText.addTextChangedListener(new TextWatcher() {


   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    // TODO Auto-generated method stub

   }


   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

   }


   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

   }
  });

Reference:Android Developers

Loading Disqus comments...
Table of Contents