Open In App

onTextChangedListener in Android

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

onTextChangedListener is a method that is called when text is edited or changed inside an EditText. TextWatcher is an essential class provided by Android Developers. Input text fields can be analyzed with it, and data on other views can be updated immediately. It can be helpful for providing instantaneous text field responses and password strength assessments in order to provide Live Updates.

Syntax:

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

To use this method:

We will create an android application in which we will add an editText and whenever the editText is edited/changed, onTextChanged method will be called then we toast a message to the user that onTextChanged method is called and the text is edited. 

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">
  
    <!-- TextView to show -->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Best website for CSE "
        android:textColor="@color/purple_500"
        android:textSize="32dp"
        app:layout_constraintBottom_toTopOf="@+id/personName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <!-- EditText which is editable -->
    <EditText
        android:id="@+id/personName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="GeeksforGeeks"
        android:ems="10"
        android:inputType="textPersonName"
        android:textColor="@color/purple_500"
        android:textColorHint="@color/purple_500"
        android:textSize="26dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java




import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    EditText gfgName;
  
    // a method to toast a message given
    public void toastMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
  
    // textWatcher is for watching any changes in editText
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // this function is called before text is edited
        }
  
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // this function is called when text is edited
            toastMsg("text is edited and onTextChangedListener is called.");
        }
  
        @Override
        public void afterTextChanged(Editable s) {
            // this function is called after text is edited
        }
    };
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register UI  element
        // with their ID
        gfgName = findViewById(R.id.personName);
  
        // set the TextChange Listener for
        // the edit text field
        gfgName.addTextChangedListener(textWatcher);
    }
}


Kotlin




import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    private lateinit var gfgName: EditText
  
    // a method to toast a message given
    fun toastMsg(msg: String?) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
  
    // textWatcher is for watching any changes in editText
    var textWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            // this function is called before text is edited
        }
  
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            // this function is called when text is edited
            toastMsg("text is edited and onTextChangedListener is called.")
        }
  
        override fun afterTextChanged(s: Editable) {
            // this function is called after text is edited
        }
    }
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // register UI  element
        // with their ID
        gfgName = findViewById(R.id.personName)
  
        // set the TextChange Listener for
        // the edit text field
        gfgName.addTextChangedListener(textWatcher)
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads