0

My requirement is to achieve this in LinearLayout. It is having only one child view. In horizontal orientation, a button needs to be at center horizontal, if layout_gravity is set. my layout xml is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="200dp"
android:background="#ffff00"
android:layout_width="match_parent"
android:orientation="horizontal">

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Button" />

</LinearLayout>

But the result is like below image

enter image description here

What i am expecting is, when the layout_gravity is set to center_horizontal , the button center to the horizontal. Why this is not working

Arundas K V
  • 801
  • 2
  • 14
  • 28

5 Answers5

1

The problem with yours is not with your actual element but with your parent element. When you define the same property to the parent element, your issue will be resolved.

add the following attribute to the LinearLayout

android:gravity="center_horizontal"
K3V
  • 282
  • 6
  • 17
1

You can achieve it in two Method,

Set android:gravity="center_horizontal" in layout properties

or

Set android:layout_gravity="center" in Button properties

Your Layout to be in "Vertical" Orientation

Brendon
  • 1,368
  • 1
  • 12
  • 28
1

We need to figure out the question is:

  • The widget can only use vertical attribute (like top, center_vertical, bottom) when it's root layout useorientation: horizontal
  • The widget can only use horizontal attribute (like left, center_horizontal, right) when it's root layout use orientation: vertical

So that you need to do like this(tested & works):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="200dp"
    android:background="#ffff00"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button" />

</LinearLayout>

Test Image

rosuh
  • 428
  • 4
  • 10
0

Set center horizontal gravity to the parent:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="200dp"
    android:background="#ffff00"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

or just change parent orientation to horizontal

dreambit.io dreambitio
  • 1,892
  • 1
  • 15
  • 24
0

Actually you cant. Your root layout is horizontal orientationL android:orientation="horizontal". If you want it work, just change root's orientation to vertical (I dont test yet!)

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86