Dimkin
Dimkin

Reputation: 690

layout_gravity not working inside LinearLayout

I've got the following code:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="3"
        android:inputType="textMultiLine" >
    </EditText>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="1"
        android:text="@string/new_post_txt_description" />
</LinearLayout>

Apparently android:layout_gravity="top" does not moves the TextView to the up. Anyone knows how to achieve that?

P.S

I saw some ideas about using RelativeLayout, but in my case i need both of the controls to be next to each other and use weight attribute as in the example.

Thanks!

Upvotes: 3

Views: 13018

Answers (3)

zackygaurav
zackygaurav

Reputation: 4388

layout_gravity doesn't work if you don't add android:orientation="vertical" in the Linear Layout.

For example

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

<!-- Your Code Here -->   

</LinearLayout>

Upvotes: 0

Dave.B
Dave.B

Reputation: 6662

Set the orientation on your LinearLayout to horizontal. i.e.

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

The entire purpose of a linear layout is the the subviews come one after the other based on the orientation. I believe default orientation is vertical. If you need to add more subview below those 2 then wrap those in another LinearLayout with a horizontal orientation.

Upvotes: 4

ByteMe
ByteMe

Reputation: 1476

Changing the textView to this worked for me

<TextView
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:layout_weight="1"
    android:text="TEST" />

This takes advantage of gravity instead of layout_gravity, but since the textView's height is set to fill_parent it has the same effect

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.

Upvotes: 10

Related Questions