'Constraint layout with top title, recycler view and bottom button
I am trying to implement a layout, which will contain the top title, recycler view and bottom button. Looks simple, but I need to have a wrap_content height for constraint layout to make it scalable depending on the content (top title, bottom button and recycler view. All examples that I've already found with match_parent value. Also, I need to keep the title and bottom button on the screen, where there are a lot of recycler view items. In turn recycler view must fill all available place and don't overlap title and bottom button.
So, requirements for layout:
- Title is always on the screen
- Bottom button is always on the screen
- Constraint layout must be scalable depending on the recycler view content (picture below)
The concept:
Layout which I have 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="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:background="@color/yellow"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="16dp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvList"
android:layout_width="wrap_content"
android:layout_height="@dimen/constraint_parent"
app:layout_constraintBottom_toTopOf="@id/btnSave"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle"
tools:itemCount="50" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSave"
android:layout_width="@dimen/constraint_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result - recycler view isn't displayed

Perhaps there is another way to do this, but I spent a lot of time looking for a solution. I believe the solution should be simple, but I haven't found one yet. DialogFragment doesn't fit here because this screen needs to be in full-screen mode when the app is in portrait orientation.
Solution 1:[1]
Just constraint the bottom button (btnSave) as - app:layout_constraintTop_toBottomOf="@id/rvList"
Also don't fix the height of the recyclerView. Keep it as wrap_content
Solution 2:[2]
Try to set android:layout_height to match_parent on your ContraintLayout.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kartik Singhal |
| Solution 2 | Sam Chen |
