(updated for AndroidX)
Since the Support Library v24.2.0. you can achivie this very easy
What you need to do is just:
-
Add the design library to your dependecies
dependencies { implementation "com.google.android.material:material:1.2.1" } -
Use
TextInputEditTextin conjunction withTextInputLayout<com.google.android.material.textfield.TextInputLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/etPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true"> <android.support.design.widget.TextInputEditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password_hint" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout>
passwordToggleEnabled attribute will make the password toggle appear
-
In your root layout don’t forget to add
xmlns:app="http://schemas.android.com/apk/res-auto" -
You can customize your password toggle by using:
app:passwordToggleDrawable – Drawable to use as the password input visibility toggle icon.
app:passwordToggleTint – Icon to use for the password input visibility toggle.
app:passwordToggleTintMode – Blending mode used to apply the background tint.
More details in TextInputLayout documentation.
