Search code examples
androidandroid-layoutandroid-relativelayout

How to place 2 views to the bottom of the parent but one stacked above the other?


In a RelativeLayout if a view has android:layout_alignParentBottom="true” then it is placed to the bottom of the parent.
If 2 views have android:layout_alignParentBottom="true” then both are placed on the bottom, but 1 above the other.
How can I have 2 view to have the setting android:layout_alignParentBottom="true” and one to be stacked over the other?
android:layout_above seems to be not applicable/working for this case.


Solution

  • Put the views inside a LinearLayout with orientation - vertical, and put

    layout_alignParentBottom=true
    

    To the LinearLayout;

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:background="@color/black"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_alignParentBottom="true"
            android:background="@color/red"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    enter image description here