Search code examples
androidandroid-edittextpasswordsandroid-textinputlayout

How to display toggle button of password on/off mode in andorid


I have edit text with text input layout having toggle button. My toggle button(eye open and cross/Close) is visible but not showing on / off mode like open eye or cross eye. My code is given below:

 <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/white"
            app:errorTextAppearance="@style/PasswordErrorAppearance"
            app:passwordToggleEnabled="true"
            >

            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_password"
                android:inputType="textPassword"
                android:textColor="@android:color/white" />
        </android.support.design.widget.TextInputLayout>

and style is :

<style name="PasswordErrorAppearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">#ff0000</item>

    </style>

Solution

  • I got the answer of the above question , please follow the below process and you will get the on/off state of toggle button of show password.

    Create the TextInputLayout with EditText.

     <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/white"
            app:passwordToggleDrawable="@drawable/toggle_password"
            app:errorTextAppearance="@style/PasswordErrorAppearance"
            >
    
            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_password"
                android:inputType="textPassword"
                android:textColor="@android:color/white"
              />
        </android.support.design.widget.TextInputLayout>
    

    where app:passwordToggleDrawable="@drawable/toggle_password" add this line where toggle_password is selector xml. Now i am showing the selector xml.

    toggle_password.xml:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/open_eye" android:state_checked="true"
        android:state_pressed="true" />
    //currently pressed turning the toggle on
    <item android:drawable="@drawable/eye_blocked" android:state_pressed="true" />
    //currently pressed turning the toggle off
    <item android:drawable="@drawable/eye_blocked" android:state_checked="true" />
    //not pressed default checked state
    <item android:drawable="@drawable/open_eye" />
    //default non-pressed non-checked
    

    After following the process you will be able to get on/off state. It can be helpful for some other person.