Helenesh Helenesh - 2 months ago 43
Android Question

Add RelativeLayout with buttons below RecyclerView

I need to add a RelativeLayout below my RecyclerView and was able to do so, except that the button under TOTAL(R.id.total_amount_tv) does not show:

enter image description here

I can easily scroll through the items and it doesn't affect my RelativeLayout. I just need the button to be visible.

<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="wrap_content"
android:background="@android:color/white"
android:weightSum="1">

<android.support.v7.widget.RecyclerView
android:id="@+id/order_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
tools:context=".ShoppingCartActivity" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<TextView
android:id="@+id/total_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/total"
android:textSize="20sp"
android:textStyle="bold|italic" />

<TextView
android:id="@+id/total_amount_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/total_tv"
android:textSize="20sp"
android:textStyle="bold|italic" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/total_amount_tv"
android:layout_marginTop="51dp"
android:background="@color/accent"
android:onClick="onClickSendOrder"
android:text="@string/order_btn"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>

</RelativeLayout>

Answer

You need to divide the screen in to two Parts one for showing the Recyclerview and other for RelativeLayout

 <LinearLayout
    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"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:weightSum="10">

     <android.support.v7.widget.RecyclerView
        android:id="@+id/order_recycler"
        android:layout_weight = "8.5"
        android:layout_width="match_parent"
        tools:context=".ShoppingCartActivity" />

     <RelativeLayout
        android:layout_weight = "1.5"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginRight="10dp">

         <TextView
            android:id="@+id/total_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="Total"
            android:textSize="20sp"
            android:textStyle="bold|italic" />

         <TextView
            android:id="@+id/total_amount_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/total_tv"
            android:textSize="20sp"
            android:text="Total Right"
            android:textStyle="bold|italic" />

         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/total_amount_tv"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:onClick="onClickSendOrder"
            android:text="Button"
            android:textColor="@android:color/white"
            android:textSize="20sp" />
     </RelativeLayout>

</LinearLayout>

This will produce the following results

Picture