views:

23

answers:

1

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 ?

A: 

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, 0);
positiveButton.setLayoutParams(layoutParams);
Reuben Scratton
That worked, but only after I indertet layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); before center in parent rule. Thank you.
Alin