Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout password toggle moves text from center

While using TextInputLayout with app:passwordToggleEnabled="true" EditText text gravity isn't centered well as shown in the photo. Any help?

 <android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">

    <EditText
        android:gravity="center"
        android:id="@+id/passwordEt"
        style="@style/editTextStyle"
        android:layout_marginBottom="20dp"
        android:hint="@string/password"
        android:inputType="textPassword" />

</android.support.design.widget.TextInputLayout>

and for the text style

<style name="editTextStyle">

    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">50dp</item>
    <item name="android:layout_margin">8dp</item>
    <item name="android:background">@drawable/text_fields</item>
    <item name="android:padding">15dp</item>
</style>

enter image description here

like image 407
Nour Eldin Ahmed Avatar asked Feb 11 '18 13:02

Nour Eldin Ahmed


2 Answers

Just add transparent drawable to the left side of EditText.

<android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:passwordToggleEnabled="true">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/ic_password_space"
                    android:drawableStart="@drawable/ic_password_space"
                    android:gravity="center" />

</android.support.design.widget.TextInputLayout>

For transparent drawable you can use whatever you want with android:width="48dp".

Create drawable resource file @drawable/ic_password_space.xml.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="1dp"
        android:viewportWidth="1.0"
        android:viewportHeight="1.0">

    <path
        android:pathData="M"
        android:fillColor="#0000"/>
</vector>
like image 111
Yeldar Nurpeissov Avatar answered Nov 15 '22 08:11

Yeldar Nurpeissov


Add empty 48dp drawable on both sides of the edit text if you hiding passwordToggle when the editText is empty, Otherwise, the cursor would not be in center initially.

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="true">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/ic_password_toggle_space"
                android:drawableEnd="@drawable/ic_password_toggle_space"
                android:gravity="center" />

for creating empty drawable you can add a blank vector ic_password_toggle_space.xml file in drawable resources:-

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp"
    android:height="1dp"
    android:viewportWidth="1.0"
    android:viewportHeight="1.0">

    <path
        android:pathData="M"
        android:fillColor="#0000"/>
</vector>
like image 25
japanjot singh Avatar answered Nov 15 '22 07:11

japanjot singh