Android TextWatcher EditText

When you create an Android application that uses the EditText to allow user's input, you might want to detect the text change and do actions accordingly. The text changed event happens when you press a key on the keyboard or you paste text into the EditText. In this tip, I am going to show you how to use the TextWatcher interface to detect the text changed in the EditText.

Now let's start by creating an example Android project. You will add one EditText and one TextView to the activity_main.xml file as shown below. The EditText allows text input. The TextView displays the text of the EditText when you make change to the text in the EditText.


<LinearLayout 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"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

     <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text|textMultiLine"
     />

   <TextView
     android:id="@+id/text_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:textSize="20sp" />

</LinearLayout>


In the MainActivity class (in MainActivity.java file), you will create a new inner class that implements the TextWatcher interface. The TextWatcher interface has three methods that must be implemented. They are beforeTextChanged, onTextChanged, and afterTextChanged. If you want to do actions before the text changed, you will write code in the beforeTextChanged. The onTextChanged allows you to do actions when the text is changed. If these methods do not meet your requirement, you can use the afterTextChanged. The code in the afterTextChanged is performed after the text in the EditText is changed. To turn on text change detection on the EditText, you need to pass an instance of the class that implements the TextWatcher interface to the addTextChangedListener method. See the example code below.

  package com.example.andtip;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView txtview;
   protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

}

  
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }
  
   public void onStart(){
   super.onStart();
   txtview=(TextView)findViewById(R.id.text_view);
   EditText editText=(EditText)findViewById(R.id.edit_text);
   editText.addTextChangedListener(new TextChangeListener());
  
  
   }
  
   private class TextChangeListener implements TextWatcher{
   //before text changed
   public void beforeTextChanged(CharSequence s, int start, int before, int count){
   Log.i("Before text changed", s.toString());
   }
   //on text changed
   public void onTextChanged(CharSequence s, int start, int before, int count){
   //display the text on the TextView
   txtview.setText(s.toString());
   }
   //after text changed
   public void afterTextChanged(Editable ed){    
   //do something after the text changed
   }

   }
}
  
   }
  
   private class TextChangeListener implements TextWatcher{
   //before text changed
   public void beforeTextChanged(CharSequence s, int start, int before, int count){
   Log.i("Before text changed", s.toString());
   }
   //on text changed
   public void onTextChanged(CharSequence s, int start, int before, int count){
   //display the text on the TextView
   txtview.setText(s.toString());
   }
   //after text changed
   public void afterTextChanged(Editable ed){    
   //do something after the text changed
   }

   }
}


Android-TextWatcher-EditText

Posted by: Dara | post date: 06-21-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.