Material-components-android: [TextInputLayout] hintEnabled set to false but hint top space is still claimed

Created on 16 Mar 2020  路  4Comments  路  Source: material-components/material-components-android

Description: Setting app:hintEnabled="false" properly hides the hint but keeps the top space where it would normally appear, causing misalignment of components.

Expected behavior:
The problem as seen in the Layout Inspector:
TextInputLayoutIssue
TextInputLayoutIssueMarked
Marked is the mentioned claimed space.

Also from the IDE preview:
With app:hintEnabled="false"
Screen Shot 2020-03-16 at 18 57 50

Without app:hintEnabled="false"
Screen Shot 2020-03-16 at 18 58 17

The expected behavior would have been basically the second the "without" visuals but without the animation and the hint.

Source code:

<com.google.android.material.textfield.TextInputLayout
          android:id="@+id/phoneLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          app:error="An error"
          app:hintEnabled="false"> <!-- This here is the culprit -->

          <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/phoneField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="03xxxxxxx"
            android:imeOptions="actionNext"
            android:inputType="phone"
            android:maxLength="10"
            android:maxLines="1" />

</com.google.android.material.textfield.TextInputLayout>

Android API version: From 23 up to recent

Material Library version: 1.2.0-alpha5

Device: Emulators, Pixel 3XL, Pixel 4XL, Samsung S9, S10, others

bug

Most helpful comment

I set top/bottom padding for EditText. But it seems that 4dp is not enough in case if you need to show error. There will be a small glitch with EditText height when error is showing. 16dp will fix it:)

All 4 comments

Have the same issue, any workarounds?

Have the same issue, any workarounds?

@agent10
Yes I have found one actually. After playing around with the Layout Inspector in Android Studio 4 I noticed that the extra space is actually padding to the TextIputEditText inside of the TextInputLayout.

If you override the paddingTop in XML, or with code, you can actually fix that. With default typography I found that a padding of 4 dp is enough to fix it, though I suggest you set both paddingTop and Bottom to make sure you are 100% symmetrical.

I set top/bottom padding for EditText. But it seems that 4dp is not enough in case if you need to show error. There will be a small glitch with EditText height when error is showing. 16dp will fix it:)

I set top/bottom padding for EditText. But it seems that 4dp is not enough in case if you need to show error. There will be a small glitch with EditText height when error is showing. 16dp will fix it:)

Was not needing the error myself so highly possible. Thanks for sharing the info.

Was this page helpful?
0 / 5 - 0 ratings