Problems understanding Android XML layout_weight

Viewed 26771

I really tried to, but I can't understand how Android interprets layout_weight setting...

What I'm trying to achieve is

  • a header on top with a fixed height
  • an input area at the bottom containing an EditText and a Button
  • a content part in the middle that's taking all what's left of space

When typing I'd like to grow the EditText to a specific height and to start scrolling if the text entered exceeds the available height. Doing this I need the surrounding LinearLayout to grow together with the EditText.

If I define a specific height for the inner LinearLayout it won't grow. If I don't, the inner layout takes ALL the space instead of the ScrollView, no matter what I try using layout_weight. :(

My current XML looks like that:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="I'm a header" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="bottom">
        <LinearLayout
            android:id="@+id/itemContainer"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:padding="2dp"
        android:gravity="bottom"
        android:isScrollContainer="true"
        android:background="@drawable/steel" >
        <EditText
            android:id="@+id/edittext01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"
            android:fadingEdgeLength="60dp"
            android:maxHeight="40dp"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:layout_gravity="right"
            android:text="Send"
            android:textStyle="bold"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

Any tips are greatly appreciated!

4 Answers
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
>
<TextView
 android:text=" TextView " 
 android:textColor="#ffffff"
 android:textSize="15sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#ff0000"
 />
 <EditText
  android:text=" EditText " 
  android:textSize="15sp"
  android:layout_width="250dp"
  android:layout_weight="1"
  android:layout_height="0dp"
  />
 <Button
    android:text=" button " 
    android:textColor="#000000"
    android:textSize="15sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
  </LinearLayout>

This will help

Related