automaton
automaton

Reputation: 3128

proportional row size with 1 fixed row

In Android, I'm trying to basically create a table with 2 rows, where 1 row is say 10 pixels, and the other takes the rest of the screen. In silverlight, this is equivalent of a table with 2 rows, one on "Auto" and the other set to "*".

Is there any way to do this? I have been playing with the layout weight, but this is always a percentage, and I would like 1 row to be fixed size (wrap_content basically).

Any ideas?

edit: I tried what was suggested, but it'snot working.. So I want the first row to take up the entire space, except what row 2 took up. Row 2 consists of 2 buttons side by side, Row 1 is just a ListView. Here is what I have:

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:layout_weight="1" android:background="#FF0000">

    <ListView android:id="@+id/edit_group_listView"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />

</LinearLayout>

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:layout_weight="0" android:background="#FFFF00">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="50dip" android:stretchColumns="2">
        <TableRow>
            <Button android:text="@string/button_save" android:id="@+id/edit_group_save"
                android:layout_width="150dip" android:layout_height="50dip"
                android:enabled="false" android:layout_column="1"></Button>

            <Button android:text="@string/button_cancel" android:id="@+id/edit_group_cancel"
                android:layout_width="150dip" android:layout_height="50dip"
                android:layout_column="3"></Button>
        </TableRow>
    </TableLayout>

</LinearLayout>

When I view this all I see is the yellow linearlayout (the buttons), no listview at all. The listview has 50 items, and I can confirm it's visible by taking it out of this setup.

Upvotes: 0

Views: 1193

Answers (3)

Taranasus
Taranasus

Reputation: 535

Yep, easy actually.

First cell must have the weight of 0. Second cell must have the weight of 1 and fill the parent by width. That way it will take up the remaning space within the container it's in.

Easy! Here's an example for your convenience

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0" android:background="#FF0000"> 
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SomeText" android:id="@+id/theview"/>
    </LinearLayout>
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#FFFF00">

    </LinearLayout>

</LinearLayout>

=======================================

UPDATE

Mate you overcomplicated it like Crayze!

First. You don't need a table layout for that.

Second the problem is you set both heights of the layout to fill_parrent. So they are both fighting over for the screen size. To fix this you just have to set both the layout sizes to wrap_content. That will work just fine. Here have an example, without the table on your code.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:layout_weight="1" android:background="#FF0000">

    <ListView android:id="@+id/edit_group_listView"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />

</LinearLayout>

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="0" android:background="#FFFF00">


            <Button android:text="@string/button_save" android:id="@+id/edit_group_save"
                android:layout_width="150dip" android:layout_height="50dip"
                android:enabled="false" android:layout_column="1"></Button>

            <Button android:text="@string/button_cancel" android:id="@+id/edit_group_cancel"
                android:layout_width="150dip" android:layout_height="50dip"
                android:layout_column="3"></Button>


</LinearLayout>

Upvotes: 1

automaton
automaton

Reputation: 3128

Okay so actually I figured this out by reviewing what Taranasus wrote, which was accurate to a degree.

This is the final XML that worked:

<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:layout_weight="1" android:background="#FF0000">

    <ListView android:id="@+id/edit_group_listView"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />

</LinearLayout>

<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="0" android:background="#FFFF00">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="50dip" android:stretchColumns="2">
        <TableRow>
            <Button android:text="@string/button_save" android:id="@+id/edit_group_save"
                android:layout_width="150dip" android:layout_height="50dip"
                android:enabled="false" android:layout_column="1"></Button>

            <Button android:text="@string/button_cancel" android:id="@+id/edit_group_cancel"
                android:layout_width="150dip" android:layout_height="50dip"
                android:layout_column="3"></Button>
        </TableRow>
    </TableLayout>

</LinearLayout>

This site also helped: http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/

The problems were that: 1. The orientation had to be "vertical", which actually doesn't make sense according to the docs. 2. The second row (the fixed one) has to have a height of wrap_content, which also doesn't make sense since Google docs about weight specifically say both elements should be fill_parent.

Upvotes: 0

Mike dg
Mike dg

Reputation: 4658

Use layout weight and set the fixed one to be 0, and the other one to be any number.

Upvotes: 0

Related Questions