Search code examples
androidandroid-textinputlayout

How to customize android passwordToggleDrawable


Android support library added TextInputLayout adding several features like floating label and password toggle visisbilty

My problem is How to customize passwordVisisbilityDrawable so my own icons for visible and hidden state will be displayed.

Also when I enable passwordToggleVisibility, height will increase.


Solution

  • for change drawable, create a selector like below

    password_toggle_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_password_visible" android:state_checked="true"/>
        <item android:drawable="@drawable/ic_password_hidden"/>
    </selector>
    

    then assign to TextInputLayout like below:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/PasswordLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:passwordToggleEnabled="true"
        **app:passwordToggleDrawable="@drawable/password_toggle_drawable"**
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <android.support.design.widget.CommonTextInputEditText
            android:id="@+id/PasswordEditText"
            android:padding="0dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>
    

    for height, I used below code after inflating:

    CheckableImageButton text_input_password_toggle = (CheckableImageButton) PasswordLayout().findViewById(android.support.design.R.id.text_input_password_toggle);
    text_input_password_toggle.setMinimumHeight(0);
    PasswordEditText().setMinHeight(0);
    PasswordEditText().setMinimumHeight(0);