Button in the bottom of LinearLayout

Viewed 42

I am making an app in which I have a RecyclerView. I have also added a button, but it isn't shown. See the left photo. What I now want is to make the RecyclerView so that may button is visible, like in the photo on the right. How can I achieve this?

Under the photos you can see my xml code. I'd like the RecyclerView to be relative/dynamic.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.test.MainActivity"
    android:orientation="vertical"
    android:background="@color/colorAccent">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewMiddle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        app:cardCornerRadius="15dp">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <androidx.recyclerview.widget.RecyclerView

                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </androidx.recyclerview.widget.RecyclerView>
        </androidx.core.widget.NestedScrollView>
    </androidx.cardview.widget.CardView>


    <Button
        android:id="@+id/btnCheckout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_corner"
        android:backgroundTint="@color/colorPrimaryDark"
        android:elevation="16dp"
        android:text="START"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />



</LinearLayout>
1 Answers

The problem is that you set the CardView height to match_parent so it takes up the entire screen. It's best to use ConstraintLayout for these kinds of layouts, but you can also fix it with minimal effort like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.test.MainActivity"
    android:orientation="vertical"
    android:background="@color/colorAccent">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewMiddle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_weight="1"
        app:cardCornerRadius="15dp">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <androidx.recyclerview.widget.RecyclerView

                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </androidx.recyclerview.widget.RecyclerView>
        </androidx.core.widget.NestedScrollView>
    </androidx.cardview.widget.CardView>


    <Button
        android:id="@+id/btnCheckout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_corner"
        android:backgroundTint="@color/colorPrimaryDark"
        android:elevation="16dp"
        android:text="START"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />



</LinearLayout>

Note I only changed CardView's layout_height to 0dp and then added the following: android:layout_weight="1" Which will tell the layout to stretch as much as it can (while not covering other elements below it).

Related