4

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
  • 2,641
  • 4
  • 19
  • 28

5 Answers5

11

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>
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
4

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

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
2

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

Bharat Mallapur
  • 674
  • 9
  • 17
quiet_penguin
  • 808
  • 7
  • 18
1

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>
Khan
  • 7,585
  • 3
  • 27
  • 44
  • 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: http://3.bp.blogspot.com/-m9XTL0rL3L0/Twvfzjkkq4I/AAAAAAAAAY8/mAf39bvGses/s1600/event01.png they are in center I want the same in buttom – Samrat Mazumdar Jun 30 '12 at 07:10
  • i think u meased something properly paste my code my snap is look like http://snag.gy/CXUYL.jpg – Khan Jun 30 '12 at 07:18
1

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>