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?
<?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>
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" .
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.
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With