41

i'm trying to set the height of recycler view on wrap_content and make it respect that,but it will exceed the other widget on layout. what can i do now?

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">



    <TextView
        android:id="@+id/tvPastRounds"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="0dp"
        android:text="Past Rounds"
        android:textColor="@color/text_color_black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:clipChildren="true"
        android:maxHeight="150dp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/tvPastRounds"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvPastRounds" app:layout_constraintVertical_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
Ali Samawi
  • 1,079
  • 1
  • 9
  • 18

4 Answers4

118

i had the same issue and i find this solution: you should add this attribute to your recyclerview and it makes your recyclerview wrap_content in the constraint_layout:

app:layout_constraintHeight_default="wrap"

let me know if this solution fixed your problem.

EDIT : recycler's height should be 0dp.

EDIT 2 : in the newer versions of support library, use this code:

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
  • 12
    This works only when combining both android:layout_height="0dp" and app:layout_constraintHeight_default="wrap" in the recyclerview. – Sebas LG Mar 31 '17 at 13:15
  • 6
    I am just curious how did you guys found this solution, it isn't written anywhere. @SebasLG – Ultimo_m Jul 27 '17 at 15:55
  • 2
    @Onregs I have also found that if the RecyclerView item has a height of wrap_content this solution does not work. Have you found any workarounds? – David Jul 17 '19 at 15:24
  • Does anyone know why there's a separate logic for RecyclerView? This worked for me, but got me thinking, why does RecyclerView need this attribute to function properly In a ConstraintLayout? – capt.swag May 30 '20 at 18:46
  • If anyone have problem similar in orientation of horizontal then do same above process for width. e.g., ```app:layout_constraintWidth_default="wrap" app:layout_constrainedWidth="true" android:layout_width="wrap_content"``` – Hitesh Sarsava Jul 20 '23 at 11:23
21
app:layout_constraintHeight_default="wrap"

is deprecated in newer versions of the Support library, so consider using

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

instead.

JohnTheWalker
  • 542
  • 4
  • 20
1

I had this issue. I needed to build a UI page that contained a RecyclerView with an unknown number of elements that should scroll. The page had the the following layout:

LinearLayout
    ConstraintLayout
        TextView
        RecyclerView
        TextView

and I wanted heights to be determined procedurally for everything using match_parent and wrap_content.

I wanted the RecyclerView to use all the space available between the two TextViews.

The key was to give the RecyclerView an "impossible" constraint, by doing this:

app:layout_constraintTop_toBottomOf="@+id/topTextView"
app:layout_constraintBottom_toTopOf="@+id/bottomTextView"

and adding this:

app:layout_constrainedHeight="true"

If you need to add margin between the top TextView and RecyclerView (and/or the bottom TextView and RecyclerView) put the margin on the RecyclerView, not the TextViews.

Code Examples

Below is the actual code I'm using (with some text styling removed for readability)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#1A1A1C">

        <TextView
            android:id="@+id/selectcampus_es"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginTop="48dp"
            android:text="Pick a campus."
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_marginTop="24dp"
            android:layout_marginBottom="24dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constrainedHeight="true"
            app:layout_constraintTop_toBottomOf="@+id/selectcampus_es"
            app:layout_constraintBottom_toTopOf="@+id/comingsoon_es" />

        <TextView
            android:id="@+id/comingsoon_es"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="48dp"
            android:text="More campuses coming soon."
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

    </android.support.constraint.ConstraintLayout>
</LinearLayout>

As a final note, the HolderView also uses only wrap_content. Here's that code as well:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
        android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatTextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="15dp"
            android:text="Avondale"
            android:alpha="1"/>
</android.support.constraint.ConstraintLayout>
JHawkZZ
  • 246
  • 3
  • 8
1

For anyone who comes here and finds none of these solutions work, make sure your list item layouts have a height of wrap_content or a static dp. If they match_parent you will be scratching your head on this one for a while.

Also, no need for layout_constrainedHeight

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

works fine..

Chris River
  • 404
  • 4
  • 4