Adrian Cosmin
Adrian Cosmin

Reputation: 103

RecyclerView max_height with wrap_content

I have a Recycler View in a Constraint Layout that shows data from a database. I want the max height to be 500dp, but the problem is that when it exceeds that size, it cuts a portion from the bottom of the view. I don't know what would be the problem.

Here is the xml of the RecyclerView:

<?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="fill_parent"
    android:layout_height="wrap_content"
    android:maxHeight="500dp"
    tools:context=".histDialog">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="3dp"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The recyclerview appears in an activity styled as a dialog.

I've tried every possible thing i could think of but i can't get it to work.

Upvotes: 3

Views: 1019

Answers (1)

Adrian Cosmin
Adrian Cosmin

Reputation: 103

I found the solution. This is the XML now:

<?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="wrap_content"
    android:layout_height="wrap_content"

    tools:context=".histDialog">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        android:clipToPadding="true"
        android:padding="3dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="500dp"
        app:layout_constraintHeight_min="50dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The important part is:

        android:layout_height="0dp"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="500dp"
        app:layout_constraintHeight_min="50dp"

for the RecyclerView. Took me long enough to realise it was this simple. Hope this can help others too.

Upvotes: 3

Related Questions