2

Original question: Why layout_constrainedHeight not working after text is updated in androidx.RecyclerView

Updated question: Why layout_width of androidx.RecyclerView could affect its layout_constrainedHeight behaviour?

Old question content:


In extension of my previous question where layout_constrainedHeight is supposingly working to dynamically wrapping recyclerView, however I faced another weird issues, after text is updated inside recycler view item, it just expand to max, where I have no idea why.

layout_constrainedHeight is required because I need to do followed:

when list is short : stick button below RecyclerView

when list is long : stick button at bottom of screen and RecyclerView can properly scroll until end of list

I have this layout followed:

take note of textView id of t2, where it was wrapping button below correctly when there are empty text, once I added text into TextView android:text="Block X XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX" The recyclerView just expand anyway where it should wrap instead. Thanks for helping.

In case you need a full source code can be clone here, because I have no idea where are the root caused. Thanks. Just clone, sync , and run. You should see the problem. Thanks in advance.


New question content:

EDIT/UPDATE :

I found that simply change layout_width of the RecyclerView from match_constraint(0dp) to match_parent does the tricks. But why is that so. Can anyone explain? is that a bug or something?


activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/divider_key_in_address_default"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="@color/colorSecondaryGray"
        app:layout_constraintBottom_toTopOf="@id/rv_user_address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user_address"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="20dp"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/ll_add_new_address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider_key_in_address_default"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed"
        tools:itemCount="10" />

    <LinearLayout
        android:id="@+id/ll_add_new_address"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rv_user_address"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:src="@drawable/ic_add" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Block X, XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX" />

        <!--        style="@style/TextViewBlue"-->

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

This is just item layout for recycler view item_simple.xml

<?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"
    android:layout_marginTop="20dp">

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="address_additional_address_title"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@id/ll"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/t1">

        <RadioButton
            android:id="@+id/r"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginVertical="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/t2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/t2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="Block X XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/iv_edit_address"
            app:layout_constraintStart_toEndOf="@id/r"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/iv_edit_address"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:padding="15dp"
            android:tint="@color/colorOrange"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/t2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@drawable/ic_edit" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

My gradle.build


    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.0"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

enter image description here

elliotching
  • 972
  • 1
  • 10
  • 33

2 Answers2

0

You should edit an activity_main.xml.

The code is worked for me as you expected below

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/divider_key_in_address_default"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="@color/colorSecondaryGray"
        app:layout_constraintBottom_toTopOf="@id/rv_user_address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user_address"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintBottom_toTopOf="@id/ll_add_new_address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider_key_in_address_default"
        tools:itemCount="10" />

    <LinearLayout
        android:id="@+id/ll_add_new_address"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rv_user_address">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:src="@drawable/ic_add" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Block X, XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX" />

        <!--        style="@style/TextViewBlue"-->

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Ashton Yoon
  • 172
  • 2
  • 6
0

PS. Sorry for removing any margins

It works exactly!!

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/divider_key_in_address_default"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="@color/colorSecondaryGray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_bias="0"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/ll_add_new_address"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="100" />

    <LinearLayout
        android:id="@+id/ll_add_new_address"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@+id/rv_user_address"
        app:layout_constraintBottom_toBottomOf="parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:src="@drawable/ic_add" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Block X, XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX" />

        <!--        style="@style/TextViewBlue"-->

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Ashton Yoon
  • 172
  • 2
  • 6