2

I'm trying to have a menu of 2 buttons vertically. I set the layout to RelativeLayout . The first button is centered by android:layout_centerVertical="true" android:layout_centerHorizontal="true" which works.

when I tried to add the second button to be under the first button by android:layout_toBottomOf="@+id/menuat" it gives me a error.

How would I center more then one button on the screen?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backFeetGallery"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <Button
      android:id="@+id/menua"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:text="But A" 
  />

  <Button
      android:id="@+id/menub"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:layout_toBottomOf="@+id/menuat"
      android:text="But B" 
  />

</RelativeLayout>
Jano
  • 62,815
  • 21
  • 164
  • 192
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • You can try `LinearLayout` , see this answer http://stackoverflow.com/questions/4189883/center-two-buttons-horizontally – wodong Sep 09 '12 at 00:52

1 Answers1

3

layout_toBottomOf isn't a valid parameter. Use layout_below instead.

Here's the amended code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backFeetGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <Button
      android:id="@+id/menua"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:text="But A" 
  />

  <Button
      android:id="@+id/menub"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/menua"
      android:text="But B" 
  />

</RelativeLayout>

By replacing this code you would get menub to be below menua

The full set of relative positions are:

android:layout_toLeftOf
android:layout_toRightOf
android:layout_above
android:layout_below
Max Rasguido
  • 457
  • 6
  • 27
David Underwood
  • 4,908
  • 1
  • 19
  • 25