6

I have a single button in Linear layout and I want to keep it at the center (horizontally).
I set android:gravity="center_horizontal" for button and linear layout but no luck.

 <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearLayout4" android:layout_gravity="center_horizontal">
        <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="center_horizontal" android:text="Search"></Button>
    </LinearLayout>

Sorry for such a basic question but as far as I know only android:gravity can be used to bring it to center and it didn't work for me.

Solution:

Thanks to Sbossb

<RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:id="@+id/Button01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Search"
            android:layout_centerInParent="true"></Button>
    </RelativeLayout>

~Ajinkya.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161

4 Answers4

11

You could use a relative layout and set the child to android:layout_centerInParent="true". LinearLayout are a little tricky when it comes to centering check out this article http://sandipchitale.blogspot.com/2010/05/linearlayout-gravity-and-layoutgravity.html.

Stefan Bossbaly
  • 6,682
  • 9
  • 53
  • 82
  • change android:layout_width="wrap_content" android:layout_height="wrap_content" in the relative layout to android:layout_width="fill_parent" android:layout_height="fill_parent" – Stefan Bossbaly Jun 21 '11 at 15:46
5

Try using this layout!

<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
    <LinearLayout android:id="@+id/linearLayout2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="match_parent">
        <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
</LinearLayout>
C--
  • 16,393
  • 6
  • 53
  • 60
  • It wrappes the button with another Linear layout with horizontal orientation. Thus it will span the parent and allows the center gravity to work. Thanks – C-- Jun 21 '11 at 15:50
4

It should be android:layout_gravity instead of android:gravity.

JDx
  • 2,615
  • 3
  • 22
  • 33
  • Should I use it for both `button` & `linear layout` ? – Ajinkya Jun 21 '11 at 15:23
  • android:gravity can be used on the linearlayout and it sets its child views to be centered. android:layout_gravity sets itself to be centered in the parent view. – JDx Jun 21 '11 at 15:24
  • set layout_width on the linear/relative layout to be fill_parent and put the layout_gravity back in. – JDx Jun 21 '11 at 15:38
  • I think some other code causing this.Will put some chunk from my code in my question. – Ajinkya Jun 21 '11 at 15:46
-1

You can use the solution in this question: Center a button in a Linear layout

Basically, instead of LinearLayout, use RelativeLayout.

Community
  • 1
  • 1
SERPRO
  • 10,015
  • 8
  • 46
  • 63