Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout - RecyclerView below Button not respecting constraints

I have a RecyclerView and a button inside ConstraintLayout. I want the Button to be at the top and the RecyclerView below it. Tried setting app:layout_constraintTop_toBottomOf="@+id/button" to RecyclerView, but it's not respecting. It looks like this:

Result view

I want the RecyclerView below the Button. Here is my code:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginTop="16dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>

I am setting start, top, end and bottom constraints to parent for Button. For RecyclerView, start and end to parent and top constraint to the bottom of button

What am I doing wrong?

like image 776
Srijith Avatar asked Nov 03 '17 18:11

Srijith


2 Answers

I think this should fix it:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_marginTop="16dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />

If your facing problems using the layout use relative or linear and convert it.

Edit:

Explanation:

  • Added

app:layout_constraintBottom_toBottomOf="parent"

to constraint recyclerview to bottom of parent.

  • zeroed the width and height to remove any overriding conflicts.
like image 85
Mohammad Tabbara Avatar answered Sep 30 '22 09:09

Mohammad Tabbara


From Build a Responsive UI with ConstraintLayout:

You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).

like image 28
Eugene Brusov Avatar answered Sep 30 '22 10:09

Eugene Brusov