Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa
Solution 1
Add an extra attribute to that EditText
programmatically and you are done:
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
For numeric password (pin):
password.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
Also, make sure that the cursor is at the end of the text in the EditText
because when you change the input type the cursor will be automatically set to the starting point. So I suggest using the following code:
et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
et_password.setSelection(et_password.getText().length());
When using Data Binding, you can make use of the following code:
<data>
<import type="android.text.InputType"/>
.
.
.
<EditText
android:inputType='@{someViewModel.isMasked ?
(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) :
InputType.TYPE_CLASS_TEXT }'
If using Kotlin:
password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
Solution 2
use this code to change password to text and vice versa
mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// hide password
mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// show password
mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
for full sample code refer http://www.codeproject.com/Tips/518641/Show-hide-password-in-a-edit-text-view-password-ty
Solution 3
password.setInputType(InputType.TYPE_CLASS_TEXT | inputType.TYPE_TEXT_VARIATION_PASSWORD);
Method above didn't really work for me. Answer below works for 2.2 sdk.
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
Set inputType for an EditText?
Solution 4
Another simple example using ImageView to toggle visibility with less code, because of single InputType assign we need only equality operator:
EditText inputPassword = (EditText) findViewById(R.id.loginPassword);
ImageView inputPasswordShow = (ImageView) findViewById(R.id.imagePasswordShow);
inputPasswordShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(inputPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
inputPassword.setInputType( InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
}else {
inputPassword.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
}
inputPassword.setSelection(inputPassword.getText().length());
}
});
Replacing :
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
With :
InputType.TYPE_CLASS_TEXT
Will give the same result but shorter word.
Solution 5
Checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is checked.
if (isChecked) {
//password is visible
PasswordField.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//password gets hided
passwordField.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
Related videos on Youtube
![Rajkiran](https://i.stack.imgur.com/ZwzFL.jpg?s=256&g=1)
Comments
-
Rajkiran almost 2 years
In my application, I have an
EditText
whose default input type is set toandroid:inputType="textPassword"
by default. It has aCheckBox
to its right, which is when checked, changes the input type of that EditText to NORMAL PLAIN TEXT. Code for that ispassword.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 orientation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).
Any way to achieve this?
-
Mahendran almost 12 yearsHow to set email type for edit text mailEdt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS); not seems to work.
-
Rajkiran almost 12 yearsUse
mailEdt.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
. Works for me.
-
-
Rajkiran over 12 yearsNot working. I even tried putting
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
immediately after initialization, but alas! -
Rajkiran over 11 yearsPlease read the question first. I want to toggle between password field and textfield. This will give me numeric textfield.
-
mente over 10 yearsas noted for correct answer - instead of changing selection you can use
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
-
Rafael Sanches about 10 yearsi don't understand why in the world android chooses to move the cursor, it just makes developers life much more difficult.
-
Ahmed Adel Ismail over 9 yearsHideReturnsTransformationMethod.getInstance() showed password, and PasswordTransformationMethod.getInstance() hide password ... implementation is correct but the comments are reversed
-
Josh over 9 yearsThis is the best answer, to complete it, just move the cursor to the last character with: txtpassword.setSelection(txtpassword.getText().length());
-
Rajkiran over 9 yearsHow is it different from the accepted answer? Please read the question and the answer carefully before spamming.
-
nAkhmedov over 9 years+1 for here is best answer
-
Ajay over 9 yearsFor a moment it seems not working due to point highlighted by EL-conte De-monte TereBentikh. Josh suggestion is also helpful. This is best answer.
-
sud007 over 8 years@PKR check the implementation buddy, which in this case is ready to use! :)
-
sunil y about 7 yearshow to use this code
if (edittext.getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD )){ edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD ); }else{ edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD ); }
for Visual Studio/Xamarin..? -
sunil y about 7 yearsi mean how to get the inputtype of the edittext in if() condition..?
-
sunil y about 7 years@sud007 do u have any idea that how to write the if condition in Xamarin/visual Studio..?
-
Vlado Pandžić about 7 yearsHow to write | in layout file when using data binding.I tried this: android:inputType='@{oneField.IsMasked==true ? (InputType.TYPE_CLASS_TEXT | inputType.TYPE_TEXT_VARIATION_PASSWORD) : InputType.TYPE_CLASS_TEXT }' It doesn't compile
-
Rajkiran about 7 years@Vlado It works for me using Data Binding as well. I have updated my answer.
-
Vlado Pandžić about 7 yearsYes, I see . I had a typo
-
Samir almost 7 yearsSimple and best thnks
-
JM Lord almost 7 yearsIf the user is editing the middle of the password field, this will constantly put the cursor at the end. I'd recommend using
editText.getSelectionStart()
andeditText.getSelectionEnd()
withsetSelection(start, end)
to avoid this issue. -
Mark over 6 yearsPeople shouldn't even be looking further down. This is the best answer and would suggest that you make @JM Lord's suggested change.
-
Tzegenos over 6 yearsYou are THE professor my friend! With this solution you are not facing issues with font after changing back to Password mode (secure text).
-
CoolMind almost 6 yearsAnd this will only hide password symbols?
-
CoolMind almost 6 yearsThanks for
edtPassword.getSelectionStart()
. -
android_dev over 5 yearsThanks for Kotlin!
-
林果皞 over 5 yearsThis answer better than accepted answer
setInputType
which cause hint added extra spacing between characters. -
林果皞 over 5 yearsBut still, you should define
android:inputType="textPassword"
as default in xml or else evenandroid:maxLines="1"
will not works to make it single line. -
KSR over 4 yearsExcellent answer. Thanks to Rajkiran.
-
Alex Cohn almost 4 yearsThis way is better than
setInputType
, because it preserves the keyboard layout (see the pictures) -
sud007 almost 4 yearsNice and updated answer! Kudos for adding kotlin code!
-
Sfili_81 about 3 yearsWelcome to Stack Overflow. Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.