2

I have a horizontal RecyclerView inside a ConstraintLayout. I want to place a button at the end of the RecyclerView. When the recycler is empty, the button should expand to all available space. When items are added to the recycler, the button should squeeze down to a minimum.

There are some new additions to ConstraintLayout 1.1.0, like layout_constraintWidth_min, but I don't get it working. Is it supposed to work the way I intend?

Here are the relevant attributes, not including height:

<android.support.v7.widget.RecyclerView
    android:id="@+id/horizontal_recycler"
    android:layout_width="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/add">
</android.support.v7.widget.RecyclerView>

and

<Button
   android:id="@+id/add"
   android:layout_width="0dp"
   app:layout_constraintWidth_min="80dp"
   app:layout_constraintStart_toEndOf="@+id/horizontal_recycler"
   app:layout_constraintEnd_toEndOf="parent">
</Button>
user1592546
  • 1,480
  • 1
  • 14
  • 30
  • Can you clarify if you want to view the button if the recyclerView has more views then the height of the parent, or do you want it to become visible when you scrolled to the end. A sketch with the expected behaviour wold be great. – Mihai Nov 16 '17 at 14:46
  • I want it always visible (when there are more views, limit it to a minimum), but the solution for showing it at the end of the scroll might be interesting too – user1592546 Nov 16 '17 at 14:49
  • 1
    If you what it at the end you can add a footer to the recyclerView. – Mihai Nov 16 '17 at 14:51
  • if not i think the problem is that you have the recyclerView wrap content so i think you should add a max width to the recycler, and make the button take spread in the remaining space. i will comit a layout as un answer in 2 min – Mihai Nov 16 '17 at 14:54

1 Answers1

2

So here is my solution.

<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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_marginEnd="80dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="ADD"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/recyclerView"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Please note that in order to keep the recyclerView from squishing the button the margin end on the recycler should equal the button with plus any padding and margin you see fit to have.

Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
Mihai
  • 420
  • 6
  • 9