Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to anchor a button at the bottom of a relative layout in android

I'm new to the layout structure in android and I'm tasked with building an activity that has a button at the bottom (centered horiz as well) with a small bit of padding between the bottom of the button and the bottom of the visible screen.

The challenge is how to do this in relative layout and make it anchor to the same spot regardless of screen size

If anyone can point me in the right direction that would be great!

like image 715
Toran Billups Avatar asked Jun 08 '11 21:06

Toran Billups


1 Answers

The dev docs have some sample layouts and the API Demos project is great too.

The layout below will put the button at the bottom, but remember as you add more controls to a relative layout they will have to respect each other. like layout_above this button or put this button at the end and make sure it has an attribute like android:layout_below="id_of_other_bottommost_controll"

<?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" >

<Button 
        android:layout_height="wrap_content" android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:text="button"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>
like image 54
Patrick Kafka Avatar answered Sep 20 '22 04:09

Patrick Kafka