How to Implement TextWatcher in Android?
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.
Steps to Implement TextWatcher in Android
Step 1: Create an Empty Activity project
Create an empty activity Android Studio project.
Refer to Android | How to Create/Start a New Project in Android Studio?. Note that select Java as the programming language.
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.
Step 3: Working with the MainAcitvity.java 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 MainActivity.java file comments are added for better understanding.
Output: Run on Emulator