RecyclerView fill empty space inside LinearLayout

brunohpaiva Source

Background

I have a header and footer (LinearLayout) with a RecyclerView between them.

The problem

I need that the footer stay fixed at the bottom of the screen and the RecyclerView fills the empty space

enter image description here

What I've tried

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/constraintLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal"
              android:orientation="vertical"
              tools:context="br.com.sigane.coletordifal.activity.EnderecamentoActivity">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Header" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
                  android:id="@+id/produtos_list"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scrollbars="vertical" />
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Footer" />
    </LinearLayout>
</LinearLayout>

Also tried to use ConstraintLayout, but without success

androidandroid-layoutandroid-recyclerview

Answers

answered 2 months ago Sourabh #1

Try this

Add android:layout_height="0dp" and android:layout_weight="1" to the RecyclerView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/produtos_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Footer" />

    </LinearLayout>

</LinearLayout>

answered 2 months ago ant2009 #2

I don't think you need to have separate linear layouts. I think just one is enough. Just set your weights like this for your recyclerview and footer.

 <android.support.v7.widget.RecyclerView
        android:id="@+id/produtos_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:scrollbars="vertical" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
        android:text="Footer" />

answered 2 months ago ariochdivij666 #3

I recommend using the constraint layout, it is a FrameLayout on steroids. If you are using android studio 3.2, right-clicking on the root element in the design view shows a menu with the option "convert to constraintlayout"

The reason you do not want to nest layouts like this, Each nested element results in additional measure passes, nested layouts are messy and harder to inflate than flat layouts.

Highly recommend spending an hour or so reading about the constraint layout and how to use it. You will not regret it, helped my improve productivity by 2x.

Hope this is helpful.

comments powered by Disqus