Show or hide password based on a user toggle

You can create an icon to hide or show a password based on a user toggle to improve security and enhance the user experience.

Version compatibility

This implementation requires that your project minSDK be set to API level 21 or higher.

Dependencies

Show or hide a password based on user toggle

To show or hide a password based on a user toggle, create an input field for entering information and use a clickable icon for the toggle:

Key points about the code

  • Maintains the password visibility state inshowPassword.
  • Uses a TextField composable for password entry.
  • Has a clickable trailing icon, which toggles the value of showPassword.
  • Defines the visualTransformation attribute and the painter of the trailing icon by the state of showPassword.
  • Dictates the visualTransformation attribute to display or hide the password.

Results

Toggling the show and hide password icon
Figure 1. Toggling the show and hide password icon.

Parent collections

Text is a central piece of any UI. Find out different ways you can present text in your app to provide a delightful user experience.
Learn how to implement ways for users to interact with your app by entering text and using other means of input.

Have questions or feedback

Go to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts.