In my application, I have an EditText
whose default input type is set to android:inputType="textPassword"
by deault. It has a CheckBox
to its right, which is when checked, changes the input type of that EditText to NORMAL PLAIN TEXT. Code for that is
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
My problem is, when that CheckBox is unchecked it should again set the input type to PASSWORD. I've done it using-
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
But, the text inside that edittext is still visible. And for surprise, when I change the orienatation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).
Any way to achieve this?
After you
setInputType
for a password field, you will have problem with FONTHere is my solution for show/hide password without font problem
Note: I use
InputType.TYPE_TEXT_VARIATION_PASSWORD
instead ofInputType.TYPE_CLASS_TEXT
orHideReturnsTransformationMethod
because I want the keyboard display both text and numberDEMO
Another simple example using ImageView to toggle visibility with less code, because of single InputType assign we need only equality operator:
Replacing :
With :
Will give the same result but shorter word.
This is the full onClick handler for the Image/Button to show/hide the password.
Method above didn't really work for me. Answer below works for 2.2 sdk.
Set inputType for an EditText?
My search for a similar solution for Visual Studio / Xamarin lead me to this thread. Below is what worked for me with Xamarin. Note that this implementation retains the
TYPE_TEXT_FLAG_NO_SUGGESTIONS
flag when switching between modes.EditText et = FindViewById<EditText>(Resource.Id.ET);
To show characters:
et.InputType = Android.Text.InputTypes.TextVariationVisiblePassword | Android.Text.InputTypes.TextFlagNoSuggestions;
To hide characters:
et.InputType = Android.Text.InputTypes.TextVariationPassword | Android.Text.InputTypes.ClassText;
To set position to end:
int position = et.Text.Length;
et.SetSelection(position, position);
Just an additional comment on the correct answer provided by @Rajkiran, you may want to add
to the NORMAL input state so that the users wont be annoyed by the keyboard's auto-suggestion