Ksenia
Ksenia

Reputation: 3753

layout_gravity isn't working as expected - just partially

I have the following layout:

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

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

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:text="@string/title_day"
                android:textStyle="bold"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:text="Tuesday"
                android:textSize="16sp" />

        </LinearLayout>

        <TextView
            android:id="@+id/tv_fail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+22"
            android:textSize="16sp"
            android:layout_gravity="end|center_vertical" />

    </LinearLayout>

</LinearLayout>

And this looks like this:

enter image description here

And I want the last TextView with id tv_fail to be pinned to right end of screen. I suppose that

android:layout_gravity="end|center_vertical"

should handle it, but this instruction centers TextView vertically, but doesn't move it to end. How to fix it?

Upvotes: 0

Views: 116

Answers (2)

Jeel Vankhede
Jeel Vankhede

Reputation: 12118

If you just want to set text inside of your last TextView to end then you should use :

android:gravity="end"

for TextView gravity use:

android:layout_gravity="center"

Difference between android:gravity & android:layout_gravity is that first one arrange content inside of any view with given gravity while second one arranges view according to given gravity.


Updated code:

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

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:text="title_day"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:text="Tuesday"
            android:textSize="16sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/tv_fail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="+22"
        android:textSize="16sp" />

</LinearLayout>

Upvotes: 1

Ben P.
Ben P.

Reputation: 54194

I suppose that

android:layout_gravity="end|center_vertical"

should handle it, but this instruction centers TextView vertically, but doesn't move it to end.

This is due to how LinearLayout works; your understanding of layout_gravity is generally correct.

LinearLayout takes its children and lays them out in a line, and the children will all be packed towards the "start" of the LinearLayout. In other words, a horizontal LinearLayout will ignore the horizontal component of the layout_gravity attribute of a child.

There are a few ways to work around this. The one that I think works best for your scenario is to make the TextView stretch to fill all the remaining space in the LinearLayout (using layout_weight), and then have the TextView position its text at the end of its content area (using gravity).

<TextView
    android:id="@+id/tv_fail"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="+22"
    android:textSize="16sp"
    android:layout_gravity="center_vertical"
    android:gravity="end"/>

Upvotes: 1

Related Questions