Friday, October 11, 2013

Position two buttons on each side of the screen

Working with layouts in Android can sometimes be extremely simple and other times extremely difficult to accomplish seemingly simple tasks. I fought for a while with how to position two buttons on each side of the screen with a space between them. I'll spare all of my trials and tribulations, and get right to the code that makes it work...



The trick is to use a RelativeLayout and implement the following button layout properties:

  • android:layout_alignParentLeft="true"

  • android:layout_alignParentRight="true"

<?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"
android:orientation="horizontal"
android:background="@color/white" >

<Button
android:id="@+id/back_btn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"
android:padding="10dip"
android:background="@drawable/button"
android:textColor="@color/white" />

<Button
android:id="@+id/forward_btn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentRight="true"
android:background="@drawable/button"
android:textColor="@color/white"
android:padding="10dip" />

</RelativeLayout>