Boon
Boon

Reputation: 41480

Centering a button in a linear layout

Using gravity on LinearLayout centers the button correctly:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/new"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"/>
    </LinearLayout>

But using layout_gravity=center on the Button only centers the button horizontally and not vertically, why?

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEST"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:layout_gravity="center"/>
        </LinearLayout>

enter image description here

Upvotes: 2

Views: 4485

Answers (3)

RKPandroid
RKPandroid

Reputation: 1

<LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android">

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

        </LinearLayout>

Upvotes: -1

Linh
Linh

Reputation: 60923

I think the layout_gravity can make button center in LinearLayout.
Helpful article: LinearLayout gravity and layout_gravity explained
Check the simple code below. It will give you a button in center horizontal of LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f00"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button Center Hozizontal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#00f"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button Center Vertical" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0ff"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Button Center Vertical/ Horizontal" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Image Screenshot

Hope this help.

Upvotes: 5

Sweeper
Sweeper

Reputation: 271515

gravity is defined in ViewGroup (only ViewGroup and its subclass have this attribute). And layout_gravity is defined in View.

layout_gravity changes the view's gravity, so far so good. But if you apply gravity to a ViewGroup, such as a LinearLayout, the views inside the view group changes gravity.

Which means, setting gravity of a ViewGroup is the same as setting layout_gravity to all its views!

The sentence in bold certainly answers your question.

Upvotes: 0

Related Questions