material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

Exposed Dropdown Menu TextInputLayout hint overlaps text

Open VGJohn opened this issue 7 years ago • 10 comments

After following the sample for setting up an exposed dropdown menu, it appears that the hint text always overlaps the text for the selected item. Also I have to add padding to the AutoCompleteTextView otherwise the text and hint are completely aligned to the start of the TextInputLayout.

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/dropdown"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:hint="hint"
        app:helperText="helper"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <AutoCompleteTextView
            android:id="@+id/value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="8dp"
            android:paddingEnd="8dp"
            android:text="item"
            android:editable="false" />

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

</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot 2019-07-15 at 12 03 27

Android API version: 28

Material Library version: 1.1.0-alpha08

Device: Samsung S8

VGJohn avatar Jul 15 '19 11:07 VGJohn

Seeing this as well, did you find a workaround?

It seems like the hint floating label/animation on the TextInputLayout isn't working as well. It just starts in the post-animation state instead of moving there after a selection.

cgoodroe avatar Jul 23 '19 23:07 cgoodroe

@cgoodroe No I couldn't find a way around this issue, sorry. I ended up having to make a compromise with the app design instead until it is fixed. I'm surprised something like this went unnoticed.

VGJohn avatar Jul 23 '19 23:07 VGJohn

@VGJohn I think I've figured it out. Two things are going on here.

  1. Remove all of your margins and paddings. Then add padding="16dp" on your parent ConstraintLayout. I think this will get you most of the way there.

  2. This was my main issue, where the hint didn't seem to be working properly. It turns out that the dropdown was actually grabbing focus, but it's really difficult to tell when it's not editable. I believe this is expected behavior when it's the first field in your view hierarchy. You can test this by adding a second field beneath it and tap into it, you'll see your hint drop back down into its expected state.

Hope this helps. In case it matters, my parent layout is LinearLayout, and my app themes are:

Theme.MaterialComponents.DayNight.Bridge
Theme.MaterialComponents.DayNight.NoActionBar.Bridge

cgoodroe avatar Jul 24 '19 18:07 cgoodroe

@cgoodroe Good to know. I do need the margins around the view and the padding in the AutoCompleteTextView is to actually stop the hint and text from being placed completely against the left side of the drop down box, which feels like another bug. Also changing the AutoCompleteTextView to be editable didn't make any difference, the hint would still overlap the text.

I believe the root of the problem is the height of the TextInputLayout isn't being calculated correctly. If you look at a normal TextInputLayout with a TextInputEditText inside, the overall height is quite large to accommodate the hint text but the same height isn't being applied here when it should be. It's as if the dropdown here is removing the extra padding that's normally included for the hint in the TextInputEditText. You can see this by manually setting the height of the TextInputLayout above to be larger than normal and hint stops overlapping, only issue with this is the height has to be very large which isn't acceptable either.

VGJohn avatar Jul 24 '19 19:07 VGJohn

@VGJohn it seems my assumption in part 1 of my previous reply was incorrect anyway, as even copy pasting your exact code doesn't reproduce the issue for me. My final guess is the device, I'm developing on Pixels which are probably the least likely to expose bugs like that. Sorry I couldn't help, and good luck!

cgoodroe avatar Jul 24 '19 20:07 cgoodroe

@aliakbarmostafaei nice find and thanks for the workaround! Hopefully it gets a proper fix soon!

VGJohn avatar Sep 06 '19 00:09 VGJohn

@VGJohn Did you try giving the AutoCompleteTextView a style? Seem like style="@style/Widget.MaterialComponents.AutoCompleteTextView.FilledBox" solves your problem even without the nice workaround provided above.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/dropdown"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:hint="hint"
        app:helperText="helper"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <AutoCompleteTextView
            style="@style/Widget.MaterialComponents.AutoCompleteTextView.FilledBox"
            android:id="@+id/value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="8dp"
            android:paddingEnd="8dp"
            android:text="item"
            android:editable="false"  />

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

</androidx.constraintlayout.widget.ConstraintLayout>

Using Theme.MaterialComponents.Light on this activity this gives me

image

simne7 avatar Jun 19 '20 16:06 simne7

@simne7 haven't tried that, nice job finding it though. The material.io docs should be updated to reflect this if it really is the intended way to define this component.

https://material.io/develop/android/components/menu/

VGJohn avatar Jun 19 '20 20:06 VGJohn

It should work without any problem with 1.1.0 - 1.2.0 - 1.3.0-alpha02.

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/dropdown"
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="hint"
    app:helperText="helper">

    <AutoCompleteTextView
        android:id="@+id/value"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="item" />

</com.google.android.material.textfield.TextInputLayout>
Schermata 2020-08-13 alle 08 58 57

I think that the issue is solved.

gabrielemariotti avatar Aug 13 '20 07:08 gabrielemariotti

thank you for checking! I don't see it happening on my side either so will close the issue. If anyone still finds it please reopen with a sample app.

leticiarossi avatar Aug 13 '20 18:08 leticiarossi