Cruces
Cruces

Reputation: 3129

How can you make Constraint Layout resist expansion to cover other views

I have a constraint layout, and inside it I have a header view and a footer view

Those are anchors that between them have an EditText a RecyclerView and a Button that are chained together.

The layout looks like this:

<?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="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/app_padding">


    <TextView
        android:id="@+id/header"
        style="@style/app_text.large"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="@dimen/app_padding"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Hello Header" />

    <EditText
        android:id="@+id/filter"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/list"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header"
        app:layout_constraintVertical_chainStyle="packed" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/filter" />

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/footer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/list" />


    <TextView
        android:id="@+id/footer"
        style="@style/app_text.large"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="@dimen/app_padding"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="Hello footer" />
</android.support.constraint.ConstraintLayout>

This looks as I want it to, a list with multiple items that is always centered on the screen.

The problem is that when the list is populated with too many items, it starts to expand upwards and covers the header.

To setting the list's layout_height to 0dp solves the problem , but then the list always takes up most of the screen (even with too few items) and the Button is always stuck above the footer and the EditText is always stuck below the header.

Is there a way to make the RecyclerView have wrap_content as its layout height but be constrained in some way to never become so big as to cover the header?

I would prefer a solution that doesn't involve setting a max height because this is supposed to run on multiple screen dimensions

Upvotes: 2

Views: 1352

Answers (1)

Pawel Laskowski
Pawel Laskowski

Reputation: 6346

What you need is app:layout_constrainedHeight="true" attribute set on your RecyclerView. This allows to use wrap_content on the view's height yet keep enforcing the constraints to limit the resulting height.

Upvotes: 4

Related Questions