views:

109

answers:

2

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.

A: 

Did you try with setTransformationMethod? It's inherited from TextView and want a TransformationMethod as a parameter.

You can find more about TransformationMethods here.

It also has some cool features, like character replacing.

Hamcha
+1  A: 

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.setTransformationMode(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.setTransformationMode(new DoNothingTransformation());
Janusz
Thanks. yourTextView.setTransformationMethod(new PasswordTransformationMethod()); Does exactly what I need. I also have not tested if the method still works after the view is displayed.
JackN