Android – EditText TextWatcher

By -

Today we are going to discuss about TextWatcher which can be implemented in EditText.

What is TextWatcher

  1. It is an Interface. It has 3 methods to be overrided whenever it is implemented:
    • afterTextChanged
    • beforeTextChanged
    • onTextChanged
  2. It 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.

For Example: To ensure user can enter only particular length of characters in the EditText.

Now, let me explore an example of TextWatcher interface:

EditText mPasswordLength;
mPasswordLength = (EditText)findViewById(R.id.password_length);
mPasswordLength.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)
{
}
});

For practical example: visit this StackOverflow question, you can come to understand easily about this: http://stackoverflow.com/questions/6242661/enter-data-in-edittext-and-respond-without-pressing-enter/6242778#6242778

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...