Reputation: 362
I am trying to achieve this layout
<RelativeLayout>
<Button> <!-- Always attached to the top of the parent -->
<RecyclerView>
<Button> <!-- Always attached to the bottom of the parent -->
<RelativeLayout>
Here is the code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
My problem is that although the views are placed correctly, whenever I scroll to the last item in the RecyclerView
, it is partially hidden behind the bottom Button
. How can I place RecyclerView
exactly between the 2 buttons no matter how many items are there in the RecyclerView
?
Upvotes: 0
Views: 1091
Reputation: 416
You need to tell the RecyclerView to be below and above your buttons.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/topButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTemperatureLog"
android:layout_above="@+id/bottomButton"
android:layout_below="@+id/topButton"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<Button
android:id="@+id/bottomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Upvotes: 2
Reputation: 2512
You must use LinearLayout
as a parent when you are specifying layout_weight
attributes :
<?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:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</LinearLayout>
Upvotes: 1
Reputation: 26198
You can always specify the position of the recycler view inside the relative layout. Where you can put it on top, bottom or both of a view.
First of all you need to add ids to the buttons and then you can specify the position of the recycler view
android:layout_below="@+id/button_top" // place the view below the specified view id
android:layout_above="@+id/button_bottom" // place the view above the specified view id
Click here see the full list on how to position the view inside the RelativeLayout
Upvotes: 0