material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.25k stars 3.06k forks source link

[TextInputLayout] No focus in ViewPager2 when width is set using ConstraintLayout constraints #1730

Closed wondering639 closed 2 years ago

wondering639 commented 4 years ago

Description: Put a TextInputLayoutin a ConstraintLayoutand put that into a ViewPager2via a Fragment. If the TextInputLayoutwidth is given as match_parent, it will receive focus as expected. However, if the width of the TextInputLayoutis given with 0dp and constraints, focus won't work.

Expected behavior: Focus has to be possible no matter which way the TextInputLayoutwidth was set.

Source code: Sample at https://github.com/wondering639/viewpager2-focus-issue 1) Touch the TextInputLayout on the first tab, the TextInputLayout won't get a blue border: image

2) Touch the TextInputLayout on the second tab, the TextInputLayout will get a blue border. image

Only difference between the first and second tab is that for the TextInputLayoutin the fragment of the first tab, the width is given with 0dp and constraints. On the second tab the width for the TextInputLayoutin the fragment is given as match_parent.

So: fragment_first.xml (focus not working)

    <com.example.viewpager2focus.MyTextInputLayout
        android:id="@+id/textinputlayout_fragment1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

fragment_second.xml (focus working)

    <com.example.viewpager2focus.MyTextInputLayout
        android:id="@+id/textinputlayout_fragment2"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"  <----- THE ONLY DIFFERENCE
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

Android API version: 30, but same on API 29

Material Library version: Material Android Library version you are using here (e.g., 1.1.0-alpha07) com.google.android.material:material:1.2.1

Device: Pixel 2 / Emulator API 30

francisnovotny commented 3 years ago

same issue in RecyclerView

francisnovotny commented 3 years ago

It can be fixed by updating Constraint layout dependency to 2.0.2:

implementation 'androidx.constraintlayout:constraintlayout:2.0.2'