Friday, 19 April 2013

What is a TEXTWATCHER in ANDROID ?

TextWatcher is used to keep watch on EditText while entering data into it. We can perform
operation and keep watch on which characters are being entered or how many characters are
being entered in the EditText.  

Text Watcher has three events.
 1.beforeTextChanged : This means that the characters are about to be replaced with some
 new text. The text is uneditable. This event is used when you need to take a look at the old
 text which is about to change.

 2.onTextChanged: Changes have been made, some characters have just been replaced.
The text is uneditable. This event is used when you need to see which characters in the
text are new.

 3.afterTextChanged : The same as above, except now the text is editable. This event is
used when you need to see and possibly edit new text.  
Java source code is given below
 
EditText myedittext;
myedittext = (EditText)findViewById(R.id.edittext);
myedittext.addTextChangedListener(new TextWatcher() {
 public void afterTextChanged(Editable s)
   {
   }
 
 public void beforeTextChanged(CharSequence s, int start,
                                         int count, int after)
   {
   }
 
 public void onTextChanged(CharSequence s, int start, 
                                         int before, int count)
   {
   }
});

No comments:

Post a Comment