Search code examples
androidandroid-linearlayout

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


Solution

  • 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.