Introduction
In many android applications, when you open the login page the login button is disabled by default and when you fill the email and password field, the login button is automatically enabled. There are multiple ways to perform this task but the TextWatcher interface is the best way to perform this task.
TextWatcher Interface
TextWatcher interface is useful for editing text. This interface is used to watch any change made over edit text. TextWatcher interface is a combination of the following three methods-
- beforeTextChanged() Method - This method is called before making any change over edit text.
- onTextChanged() Method - This method is called while making any change over edit text.
- afterTextChanged() Method - This method is called after a change is made over edit text.
Implementation
Step 1
Create a new project in android studio and select an empty activity
Step 2
Go to activity_main.xml and add the following code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter your 6 digit password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et"
app:layout_constraintVertical_bias="0.074" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here we take one edittext and one button.
Step 3
Go to MainActivity.java and add the following code
import androidx.appcompat.app.AppCompatActivity;
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 {
private EditText editText;
private Button loginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.et);
loginBtn=findViewById(R.id.login_btn);
editText.addTextChangedListener(textWatcher);
}
TextWatcher textWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.toString().length()==6){
loginBtn.setEnabled(true);
}else{
loginBtn.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
}
Explanation
First, we create edittext and button object and initialize with findViewById() method. Now we create the TextWatcher Interface object and override all three methods of this interface. Now we apply addTextChangedListener on the edittext object. Inside the onTextChanged() method, we have applied a condition if the edittext length is six then the login button is enabled otherwise it is disabled.
Output
Conclusion
In this blog, we have seen how to use the TextWatcher interface in android. Thanks for reading and I hope you liked it. If you have any suggestions or queries about this blog, please share your thoughts. You can read my other blog and articles by clicking here.
Happy learning, friends!