Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 914
Homayoun Behzadian Avatar asked Jul 30 '17 08:07

Homayoun Behzadian


People also ask

How do I toggle visibility password in Android?

There are five XML attributes associated with the password visibility toggle. passwordToggleEnabled: This attribute has its value either true or false, so when we want the password to be toggleable, we should assign this attribute the true value. passwordToggleTint: allows giving the color the visibility toggle icon.

How do I change my toggle password in Edittext?

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.


1 Answers

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);
like image 160
Homayoun Behzadian Avatar answered Oct 04 '22 02:10

Homayoun Behzadian