Damn Vegetables
Damn Vegetables

Reputation: 12464

Can I get password toggle without TextInputEditText?

I just wanted that eye button to let user toggle password (so that they can verify what they have typed). I found a solution here in StackOverflow; it was using android.support.design.widget.TextInputEditText. The problem is that it is noticeably thicker than normal EditText.

My EditText is not inside a text input layout, because I do not want that hint animation, etc. I do not want to hard-code their height, because it may vary depending on the system language and themes.

Can I get the toggle feature with normal EditText? OR can I make TextInputEditText have the same height as EditText?

enter image description here

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="121dp"
        android:background="#66FF0000"
        android:text="This is normal EditText"
        android:inputType="textEmailAddress"/>

    <android.support.design.widget.TextInputLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="156dp"
        app:hintEnabled="false"
        app:passwordToggleEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#660000FF"
            android:hint="This is TextInputEditText"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>

</RelativeLayout>

It seems that the TextInputLayout that makes this issue. I have asked another question for it. Preventing TextInputLayout from making TextInputEditText taller

Upvotes: 1

Views: 5193

Answers (2)

sagar
sagar

Reputation: 86

You can use this library

compile 'com.github.scottyab:showhidepasswordedittext:0.8'

check this link for more information.

Upvotes: 2

Rishabh Rawat
Rishabh Rawat

Reputation: 1194

Use this Code

 public void ShowPasswordCheckBoxListener(View view) {
    boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId()) {
        case R.id.button1:
            if (!checked)
                password1.setTransformationMethod(new PasswordTransformationMethod());  //hide the password from the edit text
            else
               password1.setTransformationMethod(new DoNothingTransformation()); //show the password from the edit text
            break;
        case R.id.button2:
            if (!checked)
                password2.setTransformationMethod(new PasswordTransformationMethod()); //hide the password from the edit text
            else
                password2.setTransformationMethod(null); // another option show the password from the edit text
            break;
    }
}     //password1 and password2 are your normal edittext

Upvotes: 0

Related Questions