4

How can I set the padding between the divider line and the error text (marked with the question mark in the image) in a TextInputLayout? I tried adding the padding in the style specified as errorTextAppearance but it had no effect.

enter image description here

EDIT: Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="test.textinputlayouttest.MainActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/Error">

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter address"
            android:inputType="textEmailAddress"
            android:padding="26dp"/>
    </android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>

and the style:

<style name="Error" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/red</item>
    <item name="android:paddingBottom">300dp</item>
</style>
0

4 Answers 4

9

if you need padding only between the divider line and the error text

use the previous example just change android:padding="26dp" to - > android:layout_marginBottom="20dp"

<android.support.design.widget.TextInputLayout
                android:id="@+id/usernameWrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_marginBottom="20dp"
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress"
                    android:hint="Username"/>

            </android.support.design.widget.TextInputLayout>
1
  • 1
    But this increases the bottom margin regardless of whether error text is visible or not Nov 24, 2020 at 16:51
1

You can get the error textview with

TextView textView = (TextView) inputLayout.findViewById(android.support.design.R.id.textinput_error);

And modify the layout params.

0

Just try this way.. I think this will work..

<android.support.design.widget.TextInputLayout
            android:id="@+id/usernameWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:padding="26dp"
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="Username"/>

        </android.support.design.widget.TextInputLayout>
1
  • Nope, this did not change the padding
    – buellas
    Jan 31, 2018 at 14:18
0

Use android:layout_marginBottom="-10dp" in inner EditText

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textInputLayout"
    style="@style/Widget.Design.TextInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >
<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="-10dp"
    tools:text="Text"
    />
</com.google.android.material.textfield.TextInputLayout>

works for version: com.google.android.material:material:1.5.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.