Show/Hide Password in EditText in Android

To showing/hiding the password dots in the EditText in android, here’s the easy one-line way.



Here’s the dummy layout with an EditText and a CheckBox for toggling the password dots.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="16dp"
  android:orientation="vertical">

    <EditText
      android:id="@+id/edtPassword"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter password"
      android:inputType="textPassword" />

    <android.support.v7.widget.AppCompatCheckBox
      android:id="@+id/checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Show Password" />
      
</LinearLayout> 

Now, you can toggle the password on the OnCheckedChangeListener using this snippet.

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean value) {
        if (value)
        {
            // Show Password
            edtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        else
        {
            // Hide Password
            edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
});


If you liked this article, you can read my new articles below:


profile card
Wajahat Karim
🌍 Making the world a better place, one app at a time.
🔥 Google Developer Expert (GDE) in Android . 📱 Professional Android Developer with ~7 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.
Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan