Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent recycler view from taking up whole space

I am using RecyclerView in a fragment and have set it's height to wrap_content and with a button below it. But the problem is RecyclerView takes up the whole space and button is not visible. What is going wrong?

This is my xml file -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:background="#fff"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="17sp"
    android:layout_marginTop="20dp"
    android:visibility="gone"
    android:text="You haven't added any names yet!"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/names_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"/>

<Button
    android:id="@+id/add_name"
    android:layout_width="203dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/recycler_view"
    android:padding="15dp"
    android:textColor="#F3F3F3"
    android:textSize="17sp"/>

</LinearLayout>`
like image 428
vishalmullur Avatar asked Mar 19 '15 10:03

vishalmullur


2 Answers

You should set weight to the RecyclerView and change the height to 0dp

<android.support.v7.widget.RecyclerView
android:id="@+id/names_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical"/>
like image 68
Apurva Avatar answered Oct 11 '22 01:10

Apurva


Wrap the RecyclerView and the Button in a LinearLayout, than add

android:layout_height="0dp"
android:layout_weight="1"

to RecyclerView.

Your code should look like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#fff"
              android:orientation="vertical">

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="You haven't added any names yet!"
        android:textSize="17sp"
        android:visibility="gone"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="false"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/empty_view"
        android:orientation="vertical">

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

        <Button
            android:id="@+id/add_name"
            android:layout_width="203dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/recycler_view"
            android:padding="15dp"
            android:textColor="#F3F3F3"
            android:textSize="17sp"/>

    </LinearLayout>
</LinearLayout>
like image 28
Sara Avatar answered Oct 11 '22 02:10

Sara