If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.
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.
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.
As you said yourself, put both elements inside a RelativeLayout. Then, set the "center_horizontal" property of both elements to true, and then set the "below" property of the green element to the id of the black element. The thing is that center_horizontal centers the elements relative to their parent.
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>
You can use CENTER_IN_PARENT for relative layout.
Add android:layout_centerInParent="true"
to element which you want to center in the RelativeLayout
Arcadia, just try the code below. There are several ways to get the result you're looking for, this is one of the easier ways.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"/>
</RelativeLayout>
Setting the gravity of the RelativeLayout itself will affect all of the objects placed inside of it. In this case, it's just the button. You can use any of the gravity settings here, of course (e.g. center_horizontal, top, right, and so on).
You could also use this code below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"
android:layout_centerInParent="true"/>
</RelativeLayout>
Summarized
Adding:
android:layout_centerInParent="true"
just works on RelativeLayout, if one of the following attributes is also set for the view:
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
which alignes the child view to the parent view. The "center" is based on the axis of the alignement you have chosen:
left/right -> vertical
top/bottom -> horizontal
Setting the gravity of childs/content inside the view:
android:gravity="center"
is centering the child inside the parent view in any case, if there is no alignment set. Optional you can choose:
<!-- Axis to Center -->
android:gravity="center_horizontal"
android:gravity="center_vertical"
<!-- Alignment to set-->
android:gravity="top"
android:gravity="bottom"
android:gravity="left"
android:gravity="right"
android:gravity="fill"
...
Then there is:
android:layout_gravity="center"
which is centering the view itself inside it's parent
And at last you can add the following attribute to the parents view:
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Use the attribute android:centerInParent="true"
inside a view ,
when you want to center the view in Relative layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NWE"
android:textSize="30dp"/>
</RelativeLayout>
Its easy, don't 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"/>
To center align in one of the direction use android:layout_centerHorizontal="true" or android:layout_centerVertical="true" in the child layout
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