Align Button to the bottom of screen

38,899

Solution 1

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>

Solution 2

use android:gravity="bottom" and with relative layout use android:layout_alignParentBottom="true"

Solution 3

My understanding is that you can have a layout/set of widgets to occupy the bottom of the screen provided you specify that the other widgets above will be filling the extra space. This is done using layout weight parameter. Here I put the two buttons in to a linearlayout with orientation as required. Then the rest of the widgets above in another layout with weight = 1. The layout with weight 1 growns and does not cover the buttons.

blog post showing this in more detail.

This will look as below screen here

Solution 4

Use Below Code below any Linear Or Relative Layout.I have applied weights to equally divide buttons in two half at bottom. Njoy

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/mLlayoutBottomButtons">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom">
            <Button
                android:text="Cancel"
                android:layout_width="0sp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:id="@+id/button1" />
            <Button
                android:text="OK"
                android:layout_width="0sp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:id="@+id/button2" />
        </LinearLayout>
    </RelativeLayout>

Solution 5

try this

<?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:gravity="bottom"
   >


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1" 
    android:text="Button2" />

</RelativeLayout>
Share:
38,899
Samrat Mazumdar
Author by

Samrat Mazumdar

I talk tech

Updated on June 14, 2020

Comments

  • Samrat Mazumdar
    Samrat Mazumdar almost 4 years

    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>
    
  • Samrat Mazumdar
    Samrat Mazumdar almost 12 years
    The above code works fine, but they are not at the below of screen. I want them to be at bottom part of the screen. For ex: 3.bp.blogspot.com/-m9XTL0rL3L0/Twvfzjkkq4I/AAAAAAAAAY8/… they are in center I want the same in buttom
  • Samrat Mazumdar
    Samrat Mazumdar almost 12 years
    android:layout_width="match_parent" shows error while declaring it in buttons
  • Dipak Keshariya
    Dipak Keshariya almost 12 years
    then use "wrap_content" for that.
  • Samrat Mazumdar
    Samrat Mazumdar almost 12 years
    I want the buttons to be one below another
  • Khan
    Khan almost 12 years
    i think u meased something properly paste my code my snap is look like snag.gy/CXUYL.jpg