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>`
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"/>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With