Search code examples
androidpasswords

How to switch between hide and view password


Is there a clever way to let the user switch between hide and view password in an android EditText? A number of PC based apps let the user do this.


Solution

  • You can dynamically change the attributes of a TextView. If you would set the XML Atrribute android:password to true the view would show dots if you set it to false the text is shown.

    With the method setTransformationMethod you should be able to change this attributes from code. (Disclaimer: I have not tested if the method still works after the view is displayed. If you encounter problems with that leave me a comment for me to know.)

    The full sample code would be

    yourTextView.setTransformationMethod(new PasswordTransformationMethod());
    

    to hide the password. To show the password you could set one of the existing transformation methods or implement an empty TransformationMethod that does nothing with the input text.

    yourTextView.setTransformationMethod(new DoNothingTransformation());