I have a LinearLayout, which only contains one button. I want this button to be centered vertically and aligned to the right. I tried many ways, but I couldn't make this button centered vertically. It is always aligned to the top. I also tried to put a button in RelativeLayout, the button can not be centered vertically either.
The XML is as below. Is there anything wrong with this layout? Thanks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E8E3E4">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More"
android:layout_gravity="right" />
</LinearLayout>
Changing android:layout_gravity="right" to android:layout_gravity="right|center_vertical" didn't resolve the problem in my question.
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.
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.
You say in words that you want this to be centered vertically, but you have not said in XML that you want this to be centered vertically. You will need to adjust your android:layout_gravity
attribute to specify both right
and center_vertical
.
However, I would recommend you go back to RelativeLayout
. Use android:layout_centerVertical="true"
and android:layout_alignParentRight="true"
to make the button be centered vertically and right-aligned.
Also, bear in mind that your current LinearLayout
has android:layout_height="wrap_content"
, which means there is nothing to be centered inside. You need the container to have more space than its contents (e.g., fill_parent
) if you want centering to have any meaning.
This code will put the button in vertical center and on the right screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<Button
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Click Me" />
</LinearLayout>
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