1

I have a RelativeLayout with 5 buttons :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#212121"
tools:context="com.example.test.MainActivity"
android:id="@+id/menu"
android:gravity="center">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/main_menu"
    android:gravity="center">

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/singleplayer_text"
        android:id="@+id/singleplayer_button"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="initializeGame"
        android:clickable="true"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/multiplayer_button"
        android:layout_alignStart="@+id/multiplayer_button"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/multiplayer_text"
        android:id="@+id/multiplayer_button"
        android:layout_below="@+id/singleplayer_button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/deck_name"
        android:id="@+id/deck_button"
        android:layout_below="@+id/multiplayer_button"
        android:layout_alignLeft="@+id/multiplayer_button"
        android:layout_alignStart="@+id/multiplayer_button"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/options_text"
        android:id="@+id/options_button"
        android:layout_below="@+id/deck_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/exit_text"
        android:id="@+id/exit_button"
        android:layout_below="@+id/options_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/button"
        android:textColor="#212121" />


</RelativeLayout>

Sometime in my game , i come to the point where i want to save it.And when it is saved, there should be another button added at my menu.

     resume= new Button(this);
     resume.setId((int) Math.random() * 10);
     resume.setText("Resume");
     resume.setTextColor(Color.parseColor("#212121"));
     resume.setBackgroundResource(R.drawable.button);
     int margin = (int) (8 * scale + 0.5f);
     RelativeLayout main_menu = (RelativeLayout)findViewById(R.id.main_menu);
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
     lp.setMargins(0, margin, 0,margin);
     main_menu.addView(resume, lp);

The problem is, that this new button resume covers the singleplayer button, but i want it on top of it and all buttons should still be centered. Is this possible?

greedsin
  • 1,252
  • 1
  • 24
  • 49

2 Answers2

1

Adds a layout rule to be interpreted by the RelativeLayout. This method should only be used for constraints that don't refer to another sibling (e.g., CENTER_IN_PARENT) or take a boolean value (TRUE for true or 0 for false). To specify a verb that takes a subject, use addRule(int, int) instead.

According to your requirement doesnt fit into layout , You can remove

Remove this First Then rectify your addRule

Read RelativeLayout add rule

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

Hope this helps.

lp.addRule(RelativeLayout.BELOW, R.id.below_id);

Replace below_id with your specific id.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43