• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps RecyclerView, how to set height properly

I have a RecyclerView that I want to fill the remaining space of a screen. However, if I set the width to "wrap_content" the last few rows of the RecyclerView are not shown. It seems the RecyclerView is not calculating its height properly. Looks like others have similar issues but I haven't been able to find a fix. Any ideas?

Code:
<?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"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_txt"
        style="@style/Headers"
        android:layout_width="314dp"
        android:layout_height="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:text="@string/checklist"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/clear_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="@string/clear"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_txt" />

        <!--android:layout_height="340dp"-->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/drag_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/clear_btn"/>

</android.support.constraint.ConstraintLayout>

This is the layout for each data row used by the adapter

Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Used to display items in a list.-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <ImageView
        android:id="@+id/left_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="8dp"
        android:layout_gravity="start"
        android:contentDescription="@string/empty_image"/>

    <TextView
        android:id="@+id/text"
        style="@style/Lists"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLength="25"
        android:layout_gravity="center_horizontal"
        />

    <ImageView
        android:id="@+id/right_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="16dp"
        android:contentDescription="@string/empty_image" />

</LinearLayout>
 
You should assign bottom constraint for RecyclerView.
Try code below. I added 56dp at the bottom of RecyclerView.
Actually last records can be invisible because of improper work with Navigation bar.
Brute Force method just to add space after last records into RecyclerView.
Smart way is to correctly detect type of display and set proper flags for Navigation Bar at the bottom of screen.
Code:
<?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"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_txt"
        style="@style/Headers"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:text="@string/checklist"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/clear_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="@string/clear"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_txt" />

    <!--android:layout_height="340dp"-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/drag_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:paddingBottom="56dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/clear_btn" />

</android.support.constraint.ConstraintLayout>
 
Last edited:
I tried your suggestion but now nothing shows up in the list. Could you explain a little more about detecting the display and NavigationBar flags?
 
I downloaded your demo and it works, however, when I configure my layout the same way, my RecylerView doesn't get displayed (like 0 height). I'm starting to think it has something to do with my project using Fragments and how their getting loaded. If I zipped up my project, could I email it to you to see if you notice anything obvious?
 
I figured it out thanks to v777779. I had to make two changes.

First change was to update the RecyclerView and set its height and width to '0dp'.

Code:
<android.support.v7.widget.RecyclerView
    android:id="@+id/item_list"
    style="@style/RecyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/delete_btn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

When I made this change the RecyclerView would not display, almost like its height was '0'. I then made an update to my FrameLayout that I use to display my Fragments. I originally had the FrameLayout height set to 'wrap_context'. Once I updated to 'match_parent' the RecyclerView displayed correctly.

Code:
<!-- Layout to contain contents of main body of screen -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    app:layout_constraintTop_toBottomOf="@id/toolbar" />
[\code]

Thanks for the help
 
Back
Top Bottom