3

I have a dialog fragment that contains linear layout that involves a titleText above a RecyclerView, and at the very bottom, there's a button below the recyclerView.

Since a recyclerView expands or collapses based on the number of items the adapter sets, the button sometimes gets truncated and no longer appears to be on screen, since the recyclerView just covers the entire screen.

My question is, is there a way to set the maximum height of the recyclerView without ever hiding the button underneath. I also don't want to just give the view a random height just in case the recyclerView contains no items, and it would just be a blank section.

Please let me know if you've ever run into this issue, and how you resolved this. Thanks!

jensiepoo
  • 571
  • 3
  • 9
  • 26
  • Check this it Worked for me [link](https://stackoverflow.com/questions/42694355/how-to-set-recyclerview-max-height/62654522#62654522) – Dhyan V Jun 30 '20 at 09:53

2 Answers2

4

UPDATED You can achieve this easily using layout weights. Here's an example:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Title"
            android:textSize="21sp"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="30dp">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Submit"/>
</FrameLayout>

The Title and RecyclerView will wrap content according to contents and button will always take up bottom place.

Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
Prince Bansal
  • 1,635
  • 15
  • 24
  • This solution is essentially the same as setting a layoutHeight params, as it will still distribute 80% of the view to the recyclerView even when it's empty and should be compressed? – jensiepoo Sep 29 '16 at 23:34
  • Thanks for the update! This is a cool solution. However, essentially this button will just overlay on top of the recyclerView items. – jensiepoo Sep 30 '16 at 20:46
  • Just add a padding bottom to your recycler view. – Prince Bansal Oct 01 '16 at 02:35
  • I would never prefer this solution as it is not scalable across different screens. And it has unnecessary nesting of views. No doubt it is working (to some extent), but the required behavior can easily be achieved with simpler layout structure. – Mohammed Atif Dec 21 '16 at 10:52
  • 5
    There is no any use of weight in your layout though – Leo DroidCoder May 23 '17 at 09:40
0

I suggest using RelativeLayout as it handles the positioning of views for cases like yours, so that you can actually focus on main design.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Some title" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/title"
        android:layout_above="@+id/button"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"/>
</RelativeLayout>

Above XML code is the skeleton code for what you need. you can add margins and dimensions to control the spacing. But in any case (until you provide negative margins) your views will never overlap each other.

Main trick of using RelativeLayout is the ability to use XML tags like android:layout_below or android:layout_above or android:layout_start or android:layout_end which perfectly aligns your view the way you want.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57