148

I have a RelativeLayout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip">

    <Button
        android:id="@+id/negativeButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:textColor="#ffffff"
        android:layout_alignParentLeft="true"
        android:background="@drawable/black_menu_button"
        android:layout_marginLeft="5dip"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/> 

    <Button
        android:id="@+id/positiveButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:textColor="#ffffff"
        android:layout_alignParentRight="true"
        android:background="@drawable/blue_menu_button"
        android:layout_marginRight="5dip"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

I want to be able to programatically set for positiveButton the same effect as:

android:layout_centerInParent="true"

How can I make this programatically ?

Onik
  • 19,396
  • 14
  • 68
  • 91
Alin
  • 14,809
  • 40
  • 129
  • 218

4 Answers4

426

Completely untested, but this should work:

View positiveButton = findViewById(R.id.positiveButton);
RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
positiveButton.setLayoutParams(layoutParams);

add android:configChanges="orientation|screenSize" inside your activity in your manifest

jose920405
  • 7,982
  • 6
  • 45
  • 71
Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • 5
    That worked, but only after I indertet layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); before center in parent rule. Thank you. – Alin Oct 21 '10 at 10:08
  • 9
    I'd like to add that this worked for me as well, but I had to change layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0); to layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, -1); in my situation. Your app may require a different value in the "anchor" field. – Ben Mc Jul 29 '11 at 21:51
  • 7
    the anchor field value can be **anything but 0** to signify true at present. Source has the comparisons like `if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {` where `0` effectively evaluates to `false` – Dori Sep 28 '12 at 10:52
  • 2
    As a follow-up question does anyone know if the code in this answer can be used in animations? Like for instance animate an image from a relative left offset to a centered position etc. – Jonny Nov 05 '12 at 11:40
  • 29
    you can simply use layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT), there is no need for the second parameter, as you can check in the [documentation](http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int)) – tarmelop Jan 27 '13 at 13:18
  • Please keep in mind that this is only working in `RelativeLayout` and not with `LinearLayout`! – YBrush Jan 02 '18 at 16:24
  • Also, keep in mind that what was added should be removed before adding it again. So in order to not have many rules for one layout, you should remove the old one (layoutParams.removeRule(...)) before adding the new one. Otherwise, you may expect some weird behaviors. – goodfellow Mar 02 '19 at 04:48
  • thanks for this answer. but note that if you had set a Rule for your view in your XML you should first remove it. like layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END); – behrad May 14 '19 at 21:06
14

Just to add another flavor from the Reuben response, I use it like this to add or remove this rule according to a condition:

    RelativeLayout.LayoutParams layoutParams =
            (RelativeLayout.LayoutParams) holder.txtGuestName.getLayoutParams();

    if (SOMETHING_THAT_WOULD_LIKE_YOU_TO_CHECK) {
        // if true center text:
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        holder.txtGuestName.setLayoutParams(layoutParams);
    } else {
        // if false remove center:
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
        holder.txtGuestName.setLayoutParams(layoutParams);
    }
Juan Saravia
  • 7,661
  • 6
  • 29
  • 41
9

I have done for

1. centerInParent

2. centerHorizontal

3. centerVertical

with true and false.
private void addOrRemoveProperty(View view, int property, boolean flag){
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
    if(flag){
        layoutParams.addRule(property);
    }else {
        layoutParams.removeRule(property);
    }
    view.setLayoutParams(layoutParams);
}

How to call method:

centerInParent - true

addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);

centerInParent - false

addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);

centerHorizontal - true

addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);

centerHorizontal - false

addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);

centerVertical - true

addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);

centerVertical - false

addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);

Hope this would help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

In Kotlin , this way :

private val progressBar = ProgressBar(context)
progressBar.layoutParams =
            RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        val layoutParams : RelativeLayout.LayoutParams  =
            progressBar.layoutParams as  (RelativeLayout.LayoutParams)

        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT)
        parentView.addView(progressBar)

Where parentView can be mainlayout from tour activity.

Mori
  • 2,653
  • 18
  • 24