[android] Android RelativeLayout programmatically Set "centerInParent"

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 ?

This question is related to android android-layout android-relativelayout

The answer is


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);
    }

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.


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to android-relativelayout

How to switch from the default ConstraintLayout to RelativeLayout in Android Studio Differences between ConstraintLayout and RelativeLayout Moving from one activity to another Activity in Android RelativeLayout center vertical Android Relative Layout Align Center Android: show/hide a view using an animation How to set RelativeLayout layout params in code not in xml? How to create a RelativeLayout programmatically with two buttons one on top of the other? Percentage width in a RelativeLayout What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?