Monday, June 17, 2013

Android TextWatcher Example


Basically, a 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.

For Ex. We can use TextWatcher to have checks on the password field that minimum number of characters are entered or not, or We can check the password Strength depending on number of characters entered in Password field .

TextWatcher class has 3 methods and we need override these methods to implement our own functionality.

1: public abstract void afterTextChanged (Editable s)

This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.

2: public 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 length after. It is an error to attempt to make changes to s from this callback.

3: public 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 length before. It is an error to attempt to make changes to s from this callback.

TextWatcher Example

In this post I have given an Example in which we have a password field of type EditText , in which user will  enter his password and we will show strength of password according to the number of characters entered by the user, we also show that maximum characters has been is entered when user enter 20 characters.

We have used following parameters to check Password Strength
Password Length            Password Strengthness
        1-5                                 Easy
        6-9                               Medium
      10-14                              Strong
      15-20                             Strongest

How to Attach TextWatcher to EditText

We create an Object of TextWatcher and override required methods

Declare the Objects

EditText editTextPassword;
 TextView textViewPasswordStrengthIndiactor;

inside onCreate()


Full Code :


Android TextWatcher


                 android:text="Text Watcher Demo"
                 android:textSize="30dp" />

   <!-- EditText to get the Password -->
                   android:hint="Enter Your Password"
                   <requestFocus />

                    android:text="   Password Strength :  "

        <!--  TextView to show the Password Strength  -->
                    android:text="Not Eneterd"

Android TextWatcher

public class MainActivity extends Activity
                // Create Object of EditText and TextWatcher
                EditText editTextPassword;
                TextView textViewPasswordStrengthIndiactor;
                protected void onCreate(Bundle savedInstanceState)

                            // Get the Reference of EditText

                            // Attach TextWatcher to EditText

                // EditTextWacther  Implementation

                private final TextWatcher  mTextEditorWatcher = new TextWatcher() {
                    public void beforeTextChanged(CharSequence s, int start, int count, int after)

                                // When No Password Entered
                               textViewPasswordStrengthIndiactor.setText("Not Entered");

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


                    public void afterTextChanged(Editable s)
                                        textViewPasswordStrengthIndiactor.setText("Not Entered");
                                 else if(s.length()<6)
                                 else if(s.length()<10)
                                 else if(s.length()<15)
                                   textViewPasswordStrengthIndiactor.setText("Password Max Length Reached");


Android TextWatcher

Android TextWatcher

Android TextWatcher

Android TextWatcher



New Advance Topics:                   Android LiveWallpaer Tutorial
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation
Android AlarmManager                 Android BootReceiver                       Vibrate Phone In a Desirable Pattern    
Developing for Different Screen Sizes           Showing Toast for Longer Time       Publishing your App
How to publish Android App on Google Play

 Beginning With Android
      Android : Introduction(What is Android)                                                 Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views

Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time

TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage

Working With Incoming Calls                                       DataBase :  Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android

Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog


  1. Thank you. But instead of changing textview can we display a toast message?

  2. Thank you but instead of changing textview can we display a toast message?

  3. i want to do edittext.setText("hai") and i need to get this 'hai' from textwatcher help me thanks in advance

  4. What's up mates, pleasant paragraph and fastidious urging commented at this place, I am in fact enjoying by these.