Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa

219,225

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());       
                }
            }
        });
Share:
219,225

Related videos on Youtube

Rajkiran
Author by

Rajkiran

Sr. Application Developer

Updated on July 16, 2022

Comments

  • Rajkiran
    Rajkiran almost 2 years

    In my application, I have an EditText whose default input type is set to android:inputType="textPassword" by default. 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 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
      Mahendran almost 12 years
      How 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
      Rajkiran almost 12 years
      Use mailEdt.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYP‌​E_TEXT_VARIATION_EMA‌​IL_ADDRESS);. Works for me.
  • Rajkiran
    Rajkiran over 12 years
    Not working. I even tried putting password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD‌​); immediately after initialization, but alas!
  • Rajkiran
    Rajkiran over 11 years
    Please read the question first. I want to toggle between password field and textfield. This will give me numeric textfield.
  • mente
    mente over 10 years
    as noted for correct answer - instead of changing selection you can use InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
  • Rafael Sanches
    Rafael Sanches about 10 years
    i don't understand why in the world android chooses to move the cursor, it just makes developers life much more difficult.
  • Ahmed Adel Ismail
    Ahmed Adel Ismail over 9 years
    HideReturnsTransformationMethod.getInstance() showed password, and PasswordTransformationMethod.getInstance() hide password ... implementation is correct but the comments are reversed
  • Josh
    Josh over 9 years
    This is the best answer, to complete it, just move the cursor to the last character with: txtpassword.setSelection(txtpassword.getText().length());
  • Rajkiran
    Rajkiran over 9 years
    How is it different from the accepted answer? Please read the question and the answer carefully before spamming.
  • nAkhmedov
    nAkhmedov over 9 years
    +1 for here is best answer
  • Ajay
    Ajay over 9 years
    For 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
    sud007 over 8 years
    @PKR check the implementation buddy, which in this case is ready to use! :)
  • sunil y
    sunil y about 7 years
    how 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
    sunil y about 7 years
    i mean how to get the inputtype of the edittext in if() condition..?
  • sunil y
    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ć
    Vlado Pandžić about 7 years
    How 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
    Rajkiran about 7 years
    @Vlado It works for me using Data Binding as well. I have updated my answer.
  • Vlado Pandžić
    Vlado Pandžić about 7 years
    Yes, I see . I had a typo
  • Samir
    Samir almost 7 years
    Simple and best thnks
  • JM Lord
    JM Lord almost 7 years
    If 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() and editText.getSelectionEnd() with setSelection(start, end) to avoid this issue.
  • Mark
    Mark over 6 years
    People shouldn't even be looking further down. This is the best answer and would suggest that you make @JM Lord's suggested change.
  • Tzegenos
    Tzegenos over 6 years
    You are THE professor my friend! With this solution you are not facing issues with font after changing back to Password mode (secure text).
  • CoolMind
    CoolMind almost 6 years
    And this will only hide password symbols?
  • CoolMind
    CoolMind almost 6 years
    Thanks for edtPassword.getSelectionStart().
  • android_dev
    android_dev over 5 years
    Thanks for Kotlin!
  • 林果皞
    林果皞 over 5 years
    This answer better than accepted answer setInputType which cause hint added extra spacing between characters.
  • 林果皞
    林果皞 over 5 years
    But still, you should define android:inputType="textPassword" as default in xml or else even android:maxLines="1" will not works to make it single line.
  • KSR
    KSR over 4 years
    Excellent answer. Thanks to Rajkiran.
  • Alex Cohn
    Alex Cohn almost 4 years
    This way is better than setInputType, because it preserves the keyboard layout (see the pictures)
  • sud007
    sud007 almost 4 years
    Nice and updated answer! Kudos for adding kotlin code!
  • Sfili_81
    Sfili_81 about 3 years
    Welcome 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.