Tutorial #11: Using TextWatcher in Android

Hello everyone!

Today I will be showing you how to implement the TextWatcher interface in EditText for Android.

To begin with, I will list down three simple methods that need to be overriden each time you implement the TextWatcher interface.

1. beforeTextChanged()

This method is used when the characters are going to be replaced with some new text. In this case, the text is uneditable.

2. onTextChanged()

This method is used when the changes have taken place and the characters have been replaced . In this case too, the text is uneditable.

3. afterTextChanged()

This method is similar to the above two except that the text is now editable.

So in short a TextWatcher is used to keep a watch on EditText while entering data into it.

Now let’s proceed with our tutorial!

Step 1: Create a new Android project named AndroidTextWatcherExample with package name com.example and target platform Android 2.3.3

Step 2: Use the default MainActivity class and start writing the code!

First, implement the TextWatcher interface as:

public class MainActivity extends Activity implements TextWatcher { }

Step 3: Override the three methods as discussed above

   @Override
	public void afterTextChanged(Editable arg0) 
	{
		// TODO Auto-generated method stub
    	
	}
	
    @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
				
	}

Step 4: Now for this tutorial I would be implementing a Palindrome String checker to demonstrate the working of TextWatcher. So I add my code to the afterTextChanged method.

 @Override
	public void afterTextChanged(Editable arg0) 
	{
		// TODO Auto-generated method stub
    	 String findpalindrome=et.getText().toString();
		 String reverse="";
		 int length=findpalindrome.length();
		 
		 for (int i = length - 1 ; i >= 0 ; i-- )
			 reverse = reverse + findpalindrome.charAt(i);
		 
		 if(reverse.equalsIgnoreCase(findpalindrome) && length>0)
			 setTitle("String is a palindrome");
		 else
			 setTitle("String is not a palindrome");
				
	}

The complete code is as follows:

package com.example;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.app.Activity;

public class MainActivity extends Activity implements TextWatcher
  {
    
    private EditText et;
     
    @Override
     public void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et=(EditText)findViewById(R.id.edittext1);
        et.addTextChangedListener(this);
     } //end onCreate
	
    @Override
	public void afterTextChanged(Editable arg0) 
	{
		// TODO Auto-generated method stub
    	 String findpalindrome=et.getText().toString();
		 String reverse="";
		 int length=findpalindrome.length();
		 
		 for (int i = length - 1 ; i >= 0 ; i-- )
			 reverse = reverse + findpalindrome.charAt(i);
		 
		 if(reverse.equalsIgnoreCase(findpalindrome) && length>0)
			 setTitle("String is a palindrome");
		 else
			 setTitle("String is not a palindrome");
				
	}
	
    @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
					
	}
	   
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <EditText 
    android:layout_width="250px" 
    android:layout_height="wrap_content"
    android:textSize="18sp" 
    android:id="@+id/edittext1"> 
    </EditText>

</RelativeLayout>

Here is what the result looks like

TextWatcher in Android – Snapshot 1

TextWatcher in Android – Snapshot 2

For more information please refer: Android TextWatcher

Source code for this tutorial can be found over here

Karan Balkar About Karan Balkar
self proclaimed extraterrestrial fandroid, computer engineer, amateur gamer and die hard punk rock fan!

One thought on “Tutorial #11: Using TextWatcher in Android

  1. Pingback: KeyEvent.KEYCODE_MINUS funktioniert nicht - Android-Hilfe.de

Leave a Reply