0

How can buttons be aligned in android so that they have proper spacing above, below, and between buttons?

Current code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include 
     android:id="@+id/ActionBackupBar"
     layout="@layout/actionbar_layout" 
    />

  <Button
      android:id="@+id/btnSelectLocation"
      style="@style/ButtonText"
      android:layout_width="200dp"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5sp"
      android:layout_marginRight="2sp"
      android:layout_marginTop="0sp"
      android:text="@string/activitySelectMyLocationButton" />

<Button
    android:id="@+id/btnCurrentLocation"
    style="@style/ButtonText"
    android:text="@string/activitySelectSearchLocationButton"
    android:layout_marginBottom="5sp"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="2sp"
    />

enter image description here

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Supreet
  • 2,451
  • 8
  • 25
  • 42
  • 1. Fill_parent is deprecated, use match_parent. 2. Using sp for margins doesn't make sense. Use dp instead. 3. What do you want to achieve? How do you want to align these buttons? As in the picture? – Mark Nov 19 '13 at 14:21

2 Answers2

1

Change for RelativeLayout and after try this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
    android:id="@+id/btnSelectLocation"
    style="@style/ButtonText"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="text1" />
<Button
    android:id="@+id/btnCurrentLocation"
    style="@style/ButtonText"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/btnSelectLocation"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="test2" />
</RelativeLayout>
lfrancatto
  • 107
  • 1
  • 1
  • 10
0

Use RelativeLayout instead of LinearLayout. Then you can easily align the buttons.

Hope this can help you.

kai
  • 534
  • 4
  • 12