Justin Jenkins
Justin Jenkins

Reputation: 11

Android layout_gravity

I can't understand why the following code places my TextView in the top left corner of my screen. Based on my understanding of layout_gravity, it seems that it should place my TextView at the bottom of my screen instead.

Can anyone please shed some light on this for me? This Linear Layout, despite seeming very simplistic, has caused me numerous headaches. How can something that seems so simple, be such a royal pain to grasp?

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:layout_gravity="bottom"
            android:background="#FFF"
            />    

    </LinearLayout>

Upvotes: 1

Views: 3095

Answers (4)

HenrikS
HenrikS

Reputation: 4180

A LinearLayout set to vertical orientation will ignore all layout_gravity parameters that refers to vertical alignment. So for example "bottom" wont work but "center-horizontal" will work. (The vertical alignment is always done so that each view in the layout i placed under the other views in the layout.)

A relative layout might be the way to go or maybe you can use this code that probably is pretty close to what you want.

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:gravity="bottom"
        android:layout_weight="1.0"
        android:background="#FFF" />

</LinearLayout>

Edit This blog explains things further.

Upvotes: 2

amp
amp

Reputation: 12352

I think this code do what you want:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:background="#FF0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:background="#FFF"
        />    

</LinearLayout>

And if you want the textview on center, you can use:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:background="#FF0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:layout_gravity="center"
        android:background="#FFF"
        />    

</LinearLayout>

Hope this help you.

Upvotes: 0

Blundell
Blundell

Reputation: 76476

If that is the simplicity of your layout, you shouldn't wrap it in a LinearLayout at all.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#FFF"
    android:text="HELLO HELLO" />

If you need to wrap it though, remove the vertical orientation:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF0000" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#FFF"
        android:text="HELLO HELLO" />

</LinearLayout>

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:layout_alignParentBottom="true"
            android:background="#FFF"
            />    

    </RelativeLayout>

you can use relative layout were you can align bottom easily

Upvotes: 0

Related Questions