Android RelativeLayout programmatically Set "centerInParent"

AndroidAndroid LayoutAndroid Relativelayout

Android Problem Overview


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 ?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlinView Question on Stackoverflow
Solution 1 - AndroidReuben ScrattonView Answer on Stackoverflow
Solution 2 - AndroidJuan SaraviaView Answer on Stackoverflow
Solution 3 - AndroidHiren PatelView Answer on Stackoverflow