Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Implement TextWatcher in Android?

  • Difficulty Level : Basic
  • Last Updated : 24 Nov, 2020

If there is an application containing a login form to be filled by the user, the login button should be disabled (meaning: it shouldn’t be clickable). When the user enters the credentials of the form the button should be enabled to click for the user. So in this article, we are implementing a TextWatcher to the EditText field. Have a look at the following image to get an idea of what is the TextWatcher and how that may increase user interactivity. Note that we are going to implement this project using the Java language. 

TextWatcher in Android

Steps to Implement TextWatcher in Android

Step 1: Create an Empty Activity project

Step 2: Working with the activity_main.xml

  • Implement the Two edit text fields, one for email and one for the password.
  • Invoke the following code inside the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--this is the email edittext field-->
        android:inputType="textEmailAddress" />
    <!--this is the email password field-->
        android:inputType="textPassword" />
    <!--login button which set to be false for the enabled attribute-->
        android:text="LOGIN" />

Output UI:

TextWatcher in Android

Step 3: Working with the file

  • We can also handle both the EditTexts separately. But in this case, to reduce the lines of code, the callback listener TextWatcher is implemented, and the callback listener object is passed to the addTextChangedListener method for each of the edit text.
  • Invoke the following code inside the file comments are added for better understanding.


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    // two edit text fields
    EditText etEmail, etPassword;
    // one login button
    Button bLogin;
    // implement the TextWatcher callback listener
    private TextWatcher textWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // get the content of both the edit text
            String emailInput = etEmail.getText().toString();
            String passwordInput = etPassword.getText().toString();
            // check whether both the fields are empty or not
            bLogin.setEnabled(!emailInput.isEmpty() && !passwordInput.isEmpty());
        public void afterTextChanged(Editable s) {
    protected void onCreate(Bundle savedInstanceState) {
        // register all the UI elements 
          // with their appropriate IDs
        etEmail = findViewById(;
        etPassword = findViewById(;
        bLogin = findViewById(;
        // set the TextChange Listener for both 
          // the edit text fields

Output: Run on Emulator

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!