Search code examples
androidandroid-edittextmaterial-components-androidandroid-textinputlayoutmaterial-components

Show or Hide text using password toggle


I am doing show or hide password using below code

pwdLayout.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);

I am able to achieve show or hide password. But eye icon with cross sign is showing password and without cross sign is hiding password. I need to reverse this logic how to do this?


Solution

  • Starting from the 1.2.0 this is the default behavior:

    enter image description here enter image description here

    If you want to reverse the icon you can use something like:

        <com.google.android.material.textfield.TextInputLayout
            app:endIconDrawable="@drawable/custom_password_eye"
    

    with:

        <animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            tools:ignore="NewApi">
        
            <item
                android:id="@+id/visible"
                android:drawable="@drawable/design_ic_visibility"
                android:state_checked="true"/>
        
            <item
                android:id="@+id/masked"
                android:drawable="@drawable/design_ic_visibility_off"/>
        
            <transition
                android:drawable="@drawable/avd_show_password"
                android:fromId="@id/masked"
                android:toId="@id/visible"/>
        
            <transition
                android:drawable="@drawable/avd_hide_password"
                android:fromId="@id/visible"
                android:toId="@id/masked"/>
        
        </animated-selector>
    

    enter image description here enter image description here