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

Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

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