Java – How to use PasswordToggleEnabled to set DrawableLeft to EditText

How to use PasswordToggleEnabled to set DrawableLeft to EditText… here is a solution to the problem.

How to use PasswordToggleEnabled to set DrawableLeft to EditText

I

made a TextInputEditText for PasswordField and used DrawableLeft as an icon, and then I added a PasswordToggleEnabled(true): This action removes or hides my DrawableLeft, here’s my code:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleContentDescription="Show Password"
        app:passwordToggleEnabled="true"
        app:hintEnabled="false"
        android:id="@+id/signupPasswordlayout"
        app:passwordToggleTint="@color/edittexttint">
        <android.support.design.widget.TextInputEditText
            android:hint="Password"
            android:id="@+id/signupPassword"
            android:drawableLeft="@drawable/ic_password"
            android:drawablePadding="10dp"
            android:inputType="textPassword"
            android:drawableTint="@color/edittexttint"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        </android.support.design.widget.TextInputLayout>

So, is it possible to display both (PasswordToggle and DrawableLeft?) Thanks!

Solution

I’ve never seen this exact behavior before, but when looking at the source code of TextInputLayout, it does try to preserve the user-set drawable when applying placeholder drawables for password switching. However, like most other content in the support library to which it applies, it handles them using relative positions – i.e. start and end – rather than absolute – left and pairs.

Support libraries have always been notorious for breaking anything that specifies an absolute direction or location, so it’s no surprise that this is also an issue here.

Just change the property you set to drawableStart, not drawableLeft. Keep this in mind for anything else that involves using the support library to choose absolute or relative position and direction.

Related Problems and Solutions