Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align linearlayout to vertical center?

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>
like image 354
S.J. Lim Avatar asked Nov 25 '13 05:11

S.J. Lim


People also ask

How do you do a vertical LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do you center a LinearLayout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

How do you center an image in LinearLayout?

Try layout_gravity and gravity attributes in the LinearLayout to make all the childs to be centered by default. This above is a splash screen activity. So there are two childs - ImageView and TextView. Both are center aligned.

How do you center vertically on android?

android:gravity="center_horizontal" for align text Center horizontally. android:gravity="center_vertical" for align text Center vertically. android:gravity="center" for align text Center both vertically and horizontally.

How to center align linearlayout in Android?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

How to define a linearlayout with vertical orientation in XML?

To define a LinearLayout with vertical orientation in XML layout file, assign the attribute orientation with the value "vertical". Sample code snippet for LinearLayout with orientation=”vertical” is given below.

How to set vertical orientation for linearlayout in Kotlin?

In this tutorial, we will learn both the ways of setting vertical orientation for LinearLayout in layout XML file or in Kotlin file. To define a LinearLayout with vertical orientation in XML layout file, assign the attribute orientation with the value "vertical". Sample code snippet for LinearLayout with orientation=”vertical” is given below.

How to center a linear layout within a tablerow?

so you're designing the Linear Layout to place all its contents (TextView and Button) in its center and then the TextView and Button are placed relative to the center of the Linear Layout Try <TableRow android:gravity="center_horizontal"> This will center the inner LinearLayout within the tablerow.


2 Answers

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.

like image 98
MysticMagicϡ Avatar answered Oct 22 '22 01:10

MysticMagicϡ


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.

like image 36
Viswanath Lekshmanan Avatar answered Oct 22 '22 02:10

Viswanath Lekshmanan