Search code examples
androidxmlandroid-layoutandroid-linearlayoutandroid-relativelayout

Align Button to the bottom of screen


I have two buttons in a frame layout and want them to align at the bottom of screen one below the another.

Using android:layout_gravity="bottom" makes them overlap over each other. I have no issues doing it with Relativelayout but relativelayout doesn't supports android:layout_gravity="bottom"

My XML Layout file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scanit"
        android:layout_gravity="bottom"
        android:text="Button 1" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/inf"
        android:layout_gravity="bottom"
        android:text="Button 2" />
</FrameLayout>

Solution

  • Use Below Layout Code for that, it may help you.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/mLlayoutBottomButtons" >
        </RelativeLayout>
    
        <LinearLayout
            android:id="@+id/mLlayoutBottomButtons"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button2" />
        </LinearLayout>
    
    </RelativeLayout>