DavidNg
DavidNg

Reputation: 2836

Android: dynamically set the position of button into center of screen in RelativeLayout

There has been many questions on dynamically setting the position of a button, but I just want to change the position of a button into center of the screen. My layout is RelativeLayout.

Updated:

At the beginning, myButton has been set position in XML as:

<Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Menu" />

Any suggestion would be greatly appreciated

Upvotes: 2

Views: 9375

Answers (5)

Doaa Saleh Azab
Doaa Saleh Azab

Reputation: 1

Add this 2 lines to the component you want to center in the xml file :

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

Upvotes: 0

StenaviN
StenaviN

Reputation: 3697

See an example below (click the button and it will be moved to the center).

activity_main.xml:

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

    <Button
        android:id="@+id/my_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Menu" />

</RelativeLayout>

MainActivity.java:

public class MainActivity extends Activity {
    private View mView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mView = findViewById(R.id.my_view);
        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewGroup.LayoutParams vg_lp = mView.getLayoutParams();

                // Make sure we are in RelativeLayout
                if (vg_lp instanceof RelativeLayout.LayoutParams) {
                    RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(vg_lp);
                    rl_lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                    mView.setLayoutParams(rl_lp);
                }
            }
        });
    }
}

Upvotes: 4

Dray
Dray

Reputation: 1954

Try this,

RelativeLayout my_layout = new RelativeLayout(this);
my_layout.setId(some_value);
Button my_btn = new Button(this);
RelativeLayout.LayoutParams my_params = new RelativeLayout.LayoutParams(btn_height,btn_width);
my_params.addRule(RelativeLayout.CENTER_IN_PARENT,my_layout.getId());
my_layout.addView(my_btn,my_params);

Upvotes: 0

haawa
haawa

Reputation: 3104

Try this one:

   RelativeLayout RL = (RelativeLayout)findViewById(R.id.relative_layout);
   RL.gravity(Gravity.CENTER);

with this all subviews of RL will be in center;

   Button anyButton = new Button(this);
   RL.addSubview(anyButton);//this genereted button will be in center as well

Upvotes: 0

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9801

Try:

      ......
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) button.getLayoutParams();
      layoutParams.leftMargin = parentLayout.getWidth()/2;
      layoutParams.topMargin = parentLayout.getHeight()/2;
      button.setLayoutParams(layoutParams);
      ...... 

Upvotes: 0

Related Questions