Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center button in LinearLayout

I have this layout:

<?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" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="M\nO\nN" />

    <CheckBox
        android:id="@+id/cbmon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="T\nU\nE" />

    <CheckBox
        android:id="@+id/cbtue"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1tue"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2tues"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="W\nE\nD" />

    <CheckBox
        android:id="@+id/cbwed"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1wed"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2wed"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="T\nH\nU" />

    <CheckBox
        android:id="@+id/cbthu"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1thu"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2thu"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="F\nR\nI" />

    <CheckBox
        android:id="@+id/cbfri"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1fri"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2fri"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="S\nA\nT" />

    <CheckBox
        android:id="@+id/cbsat"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1sat"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2sat"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:text="S\nU\nN" />

    <CheckBox
        android:id="@+id/cbsun"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <TimePicker
        android:id="@+id/timePicker1sun"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />

    <TimePicker
        android:id="@+id/timePicker2sun"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="40" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/okbutton"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:text="SET"
        android:gravity="center"
        android:textSize="50sp" />

</LinearLayout>

WHY do I get this result??? I would expect the button "SET" to be centered horizontally:

enter image description here

like image 775
Lisa Anne Avatar asked Jul 06 '13 17:07

Lisa Anne


1 Answers

Set button's parent linear layout gravity to center

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/okbutton"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:text="SET"
        android:gravity="center"
        android:textSize="50sp" />

</LinearLayout>  
like image 175
kaushal trivedi Avatar answered Oct 03 '22 16:10

kaushal trivedi