I\'m trying to center a button in relative layout, is this possible? I\'ve tried the Gravity and Orientation functions but they don\'t do anything.
Try
android:layout_centerHorizontal="true"
Exactly like this, it works for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000">
<Button
android:id="@+id/btn_mybutton"
android:layout_height="wrap_content"
android:layout_width="124dip"
android:layout_marginTop="5dip"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Removing any alignment like android:layout_alignParentStart="true" and adding centerInParent worked for me. If the "align" stays in, the centerInParent doesn't work
It's really easy. Try the code below,
<RelativeLayout
android:id="@+id/second_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/first_RL"
android:background="@android:color/holo_blue_bright"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
Its easy, dont Align it to anything
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Centered Button"/>
You can use CENTER_IN_PARENT for relative layout.
Add android:layout_centerInParent="true"
to element which you want to center in the RelativeLayout
To center align in one of the direction use android:layout_centerHorizontal="true" or android:layout_centerVertical="true" in the child layout