Home > database >  Android: Show button just below to the recylerview but always on the screen
Android: Show button just below to the recylerview but always on the screen

Time:09-30

On the screen, I want to show RecyclerView which start from the top and at the end of the RecyclerView, there is a button.

  1. When RecyclerView is not occupying the entire screen then the Button will sit just below the RecyclerView.
  2. When RecylerView is scrollable then in this case button will sit on the bottom of the screen.

I tried enter image description here

enter image description here

CodePudding user response:

Your RecyclerView is centering between two other layouts. Add

app:layout_constraintVertical_bias="0.0"

to the XML for the RecyclerView to move it to the top. You can look up how bias works in the ConstraintLayout documentation.

CodePudding user response:

I think you can achieve what you want with a guideline. The steps would be the following:

  1. Create an horizontal guideline that sits at the 90% of the screen, meaning that it will be really close to the bottom, on the 10% left is where your button will be when the recycler view becomes scrollable.
  2. Set the bottom constraint of the recycler view to match the top of the guideline and the top of the recycler view to the top of the screen. This will cause that the recycler view appears on the half of your screen.
  3. Set the vertical bias of your recycler view to 0.0, this will stick the recycler view to the top.
  4. Set the top constraint of the button to match the bottom of the recycler view, this will cause that it will stick to the bottom of the recycler view, but since the recycler view (thanks to the guideline) will only grow to 90% of the screen, the button will sit at the bottom of the screen as you want.

It's important to add the following attribute to the recycler view:

app:layout_constrainedHeight="true"

The layout related to the button and recycler view should look something like:

<?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"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@ id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constrainedHeight="true"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@ id/guideline"
    app:layout_constraintVertical_bias="0.0"
    tools:itemCount="130"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button text"
    app:layout_constraintTop_toBottomOf="@ id/list"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

<androidx.constraintlayout.widget.Guideline
    android:id="@ id/guideline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.9"/>

</androidx.constraintlayout.widget.ConstraintLayout>

With few items:

enter image description here

With many items: enter image description here

  • Related