Search code examples
androidmaterial-designandroid-textinputlayoutmaterial-componentsmaterial-components-android

How to centre drawable vertically in TextInputLayout?


I have added a drawable on the left side of the TextInputLayout (as seen in TextInputEditText below) and want to add some padding below it such that it becomes centred vertically inside its parent. Is there a command or a workaround for this?

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/contact_first_name_input_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:hint="@string/contact_first_name"
                android:layout_marginEnd="8dp">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/contactFirstName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName|textCapWords"
                    android:drawableStart="@drawable/ic_person_black_24dp"
                    android:drawablePadding="8dp"
                    />

Solution

  • Use the app:startIconDrawable attribute in your TextInputLayout to add a drawable:

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

    enter image description hereenter image description here