Search code examples
androidandroid-layoutandroid-recyclerviewtextviewandroid-constraintlayout

Padding inside TextView with constraintWidth_max


I have a TextView inside of a constraint layout that I use for the layout of my items inside of a RecyclerView. Some strange behavior is going on. When the app first starts the width is correct but when I scroll up and back down then the width of the TextView has changed and is now padding the extra space.

Code

<TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="64dp"
        android:background="#C3C3C3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_max="wrap"
        tools:text="hello there" />

Original:

original

After Scrolling:

after scrolling

Is there a reason for this behavior, and how can it be fixed to the expected one?


Solution

  • I solved it by replacing the wrapping the TextView with a LinearLayout. Not entirely sure why it didn't work the first time though.

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:layout_marginEnd="64dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
    
            <TextView
                android:id="@+id/tvMessageBody"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#C3C3C3"
                tools:text="hello there" />
        </LinearLayout>