19

How can I view the value of an edittext view whose input type is "password" when i click a button and when i release the button, the text display goes back to unreadable format? Like what microsoft does in windows 8 when you click the "eye" that is next to a password field.

Thanks

F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36

5 Answers5

67

I found one of best solution for Design Support Library Users:

It is really easy to achieve with the newest Support Library v24.2.0.

What you need to do is just:

  1. Add the design library to your dependencies

    dependencies {
         compile "com.android.support:design:24.2.0"
    }
    
  2. Use TextInputEditText in conjunction with TextInputLayout

    <android.support.design.widget.TextInputLayout
        android:id="@+id/etPasswordLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:layout_marginBottom="@dimen/login_spacing_bottom">
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/fragment_login_password_hint"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>
    

The passwordToggleEnabled attribute will do the job!

  1. In your root layout don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto"

  2. You can customize your password toggle by using:

app:passwordToggleDrawable - Drawable to use as the password input visibility toggle icon.
app:passwordToggleTint - Icon to use for the password input visibility toggle.
app:passwordToggleTintMode - Blending mode used to apply the background tint.

More details in TextInputLayout documentation.

enter image description here

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
32
 yourButton.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

               switch ( event.getAction() ) {
                case MotionEvent.ACTION_DOWN: 
                   editText.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
                case MotionEvent.ACTION_UP: 
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
                }
                return true;
        }
    });
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • 1
    Ok. I'm able to see the text when I touch the button but when I release it, the text does not transform back to dots (unreadable characters). I placed a Log in the two cases and both cases are being executed correctly. – F0r3v3r-A-N00b Jan 28 '15 at 06:47
  • Try to put your finger on the "eye" button and then move it in other place and release tap. Password will be shown. – Yazon2006 Jan 17 '17 at 07:59
5

I like the current answers! Just to complement, if you have a CheckBox it is (according to me) good practice to use its OnCheckedListener instead like below:

CheckBox box = (CheckBox)findViewById(R.id.dialog_checkBox_showPassword);
EditText password = (EditText) findViewById(R.id.dialog_editText_password);
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {

            if(checked){
                password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            }
            else{
                password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
            }
        }
    });
Mazze
  • 1,354
  • 4
  • 17
  • 35
2

On pressed button, set passwordField.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);. When release the button, set passwordField.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );

Joey Chong
  • 1,470
  • 15
  • 20
0

This is what worked for me

dependencies {implementation 'com.google.android.material:material:1.2.1'}


                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:passwordToggleEnabled="true">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_password_confirm"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Password"
                        android:inputType="textPassword"/>
                </com.google.android.material.textfield.TextInputLayout>