How to align linearlayout to vertical center?

AndroidAndroid Layout

Android Problem Overview


I'm trying to align LinearLayout's vertical center which shows following pic (skycolor border) to delete button's vertical center.

so I set the gravity of id:groupNumbers to center_vertical.

but no changed.

How to align id:groupNumbers to button's center?

enter image description here

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
            android:id="@+id/groupNumbers"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_weight="0.7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/txtSelected01"
                    android:text="00"
                    android:textSize="30dp"
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
        </LinearLayout>

        <LinearLayout
                android:orientation="horizontal"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/txtSelected02"
                    android:text="00"
                    android:textSize="30dp"
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
    </LinearLayout>


    <Button
            android:id="@+id/btn_deleteNum"
            android:text="Delete"
            android:textSize="20dp"
            android:layout_weight="0.2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

Android Solutions


Solution 1 - Android

Change orientation and gravity in

<LinearLayout
    android:id="@+id/groupNumbers"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_weight="0.7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

to

android:orientation="vertical"
android:layout_gravity="center_vertical"

You are adding orientation: horizontal, so the layout will contain all elements in single horizontal line. Which won't allow you to get the element in center.

Hope this helps.

Solution 2 - Android

use android:layout_gravity instead of android:gravity

android:gravity sets the gravity of the content of the View its used on. android:layout_gravity sets the gravity of the View or Layout in its parent.

Solution 3 - Android

Use layout_gravity instead of gravity. layout_gravity tells the parent where it should be positioned, and gravity tells its child where they should be positioned.

<LinearLayout
    android:id="@+id/groupNumbers"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:layout_weight="0.7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

Solution 4 - Android

For me, I have fixed the problem using android:layout_centerVertical="true" in a parent RelativeLayout:

<RelativeLayout ... >

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

</RelativeLayout>

Solution 5 - Android

For a box that appears in the center - horizontal & vertical - I got this to work with just one LinearLayout. The answer from Viswanath L was very helpful

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/layout_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/dialog_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:text="Error"
        android:textColor="#000" />

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:text="Error-Message"
        android:textColor="#000" />


    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/message_text"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Ok" />


</LinearLayout>

Solution 6 - Android

use RelativeLayout inside LinearLayout

example:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Status"/>
        </RelativeLayout>
</LinearLayout>

Solution 7 - Android

Use android:weightSum property to the parent LinearLayout and give value 3. Then in the children LinearLayout use android:layout_weight 2 and 1 respectively. Then in the First Chil LinearLayout use android:layout_gravity="center_vertical". As shown in the following code

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="3"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:layout_gravity="center_vertical"
                        android:orientation="horizontal">
                        <TextView
                            android:id="@+id/status_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="00"
                            android:textColor="@color/red"
                            android:textSize="@dimen/default_text_size"
                            />
                          <TextView
                            android:id="@+id/status_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="00"
                            android:textColor="@color/red"
                            android:textSize="@dimen/default_text_size"
                            />
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1">
                        <Button
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:text="Delete"/>
                    </LinearLayout>
                </LinearLayout>

Solution 8 - Android

You can change set orientation of linearlayout programmatically by:

LinearLayout linearLayout =new linearLayout(this);//just to give the clarity
linearLayout.setOrientation(LinearLayout.VERTICAL);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionS.J. LimView Question on Stackoverflow
Solution 1 - AndroidMysticMagicϡView Answer on Stackoverflow
Solution 2 - AndroidViswanath LekshmananView Answer on Stackoverflow
Solution 3 - AndroidLawrence ChoyView Answer on Stackoverflow
Solution 4 - AndroidMandyView Answer on Stackoverflow
Solution 5 - AndroidGene BoView Answer on Stackoverflow
Solution 6 - AndroidSomwang SouksavatdView Answer on Stackoverflow
Solution 7 - AndroidUdara AbeythilakeView Answer on Stackoverflow
Solution 8 - AndroidBobby SebastianView Answer on Stackoverflow