user6920323
user6920323

Reputation: 141

ConstraintLayout - how to wrap over RecyclerView

I have a ConstraintLayout with an appBar (containing a toolbar), two fragments (containing a RecyclerView each) and a button at the bottom.

What I am trying to do is to wrap the height of the layout over the size of the RecyclerViews. As you can see on the picture below, every Recyclerview is taking an equal height regardless of the size of the list.

enter image description here

And this is my layout

<?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"
    android:background="@color/template_grey_light"
    tools:context=".presentation.ui.activity.MainActivity">

    <include
        android:id="@+id/AppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/fragmentTop"
        app:layout_constraintVertical_chainStyle="packed"
        layout="@layout/app_bar" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentTop"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/AppBar"
        app:layout_constraintBottom_toTopOf="@+id/fragmentBottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/fragmentTop"
        app:layout_constraintBottom_toTopOf="@+id/shuffleButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <Button
        android:id="@+id/shuffleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/template_love_red"
        android:text="Go!"
        android:layout_marginBottom="20dp"
        app:layout_constraintTop_toBottomOf="@+id/fragmentBottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

I understand I am having 0dp on the height attributes and that makes it equal in height, but then if I don't, everything will overlap at the top. Any idea on how to make it work?

Upvotes: 0

Views: 664

Answers (1)

Giuseppe Criscione
Giuseppe Criscione

Reputation: 449

Use android:layout_height="wrap_content" in your FragmentContainerView tag

Upvotes: 0

Related Questions