I have a RelativeLayout containing a pair of side-by-side buttons, which I want to be centered within the layout. I could just put the buttons in a LinearLayout and center that in the RelativeLayout, but I want to keep my xml as clean as possible.
Here's what I tried, this just puts the "apply" button in the center and the "undo" button to the left of it:
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="15sp"> <TextView android:id="@+id/instructions" android:text="@string/instructions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="15sp" android:layout_centerHorizontal="true" /> <Button android:id="@+id/apply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/apply" android:textSize="20sp" android:layout_below="@id/instructions" /> <Button android:id="@+id/undo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/undo" android:textSize="20sp" android:layout_toLeftOf="@id/apply" android:layout_below="@id/instructions" /> </RelativeLayout>
To center something in a RelativeLayout, you use android:layout_centerInParent="true" on the child. If you try to center several childs, they'll end up under/over each other.
android:layout_centerHorizontal="true" android:gravity="center" I think it's correct. so that TextView will be centered in Layout, and text will be centered in TextView.
Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.
android:gravity
will align the content inside the view
or layout
it is used on.
android:layout_gravity
will align the view
or layout
inside of his parent.
So adding
android:gravity="center"
to your RelativeLayout should do the trick...
Like this:
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="15sp"> </RelativeLayout>
Here is an extension of BrainCrash's answer. It is a non nested option that groups and centers all three horizontally and vertically. In addition, it takes the top TextView and centers it horizontally across both buttons. If desired, you can then center the text within the TextView with android:gravity="center". I also removed the margins, added color, and set the RelativeLayout height to fill_parent to highlight the layout. Tested on API 11.
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:background="@android:color/black" > <TextView android:id="@+id/instructions" android:text="TEST" android:textSize="20sp" android:background="@android:color/darker_gray" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignLeft="@+id/undo" android:layout_alignRight="@+id/apply" android:gravity="center" /> <Button android:id="@+id/apply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="APPLY" android:textSize="20sp" android:layout_below="@id/instructions" /> <Button android:id="@+id/undo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="UNDO" android:textSize="20sp" android:layout_toLeftOf="@id/apply" android:layout_below="@id/instructions" /> </RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With