Height of ListView fills the whole screen, although set as wrap_content
Asked Answered
T

2

10

i have this ListView inside a LinearLayout:

<?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        .../>

    <EditText />
      ...
    </EditText>


    <ListView
        android:id="@+id/words_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

The List is populated programmatically by a SimpleCursorAdapter:

adapter = new SimpleCursorAdapter(this, R.layout.list_row_search, mCursor, from, to);

list_row_search has a TableLayout with two TableRows:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:layout_height="1dip"
        android:background="#FF33B5E5" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/list_lesson"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="1dip"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/list_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5.25"
            android:padding="1dip"
            android:paddingRight="10dp"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/list_flex"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="12.5"
            android:padding="1dip"
            android:paddingRight="10dp" />

        <TextView
            android:id="@+id/list_rom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6.25"
            android:padding="1dip"
            android:paddingRight="10dp" />
    </TableRow>

    <View
        android:layout_height="0.1dip"
        android:background="#404040" />

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/list_related"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:paddingRight="10dp"
            android:textStyle="italic" />

        <TextView
            android:id="@+id/list_ant"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingRight="10dp" />

        <TextView
            android:id="@+id/list_engl"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingRight="10dp" />
    </TableRow>

    <View
        android:layout_height="1dip"
        android:background="#FF33B5E5" />

</TableLayout>

Now when the list gets populated, the ListView occupies the hole screen, even if only one element was found in the database. The HierarchyView in eclipse shows pretty clear, that the ListView is the one that fills out the screen.

Can you tell, where the problem is? Thanks for your time!

Tonnie answered 18/3, 2013 at 14:58 Comment(0)
V
14

You shouldn't use wrap_content for the height of a ListView. wrap_content means "make me as large as needed to hold all of my children." When you consider that your data set could be potentially very large, that should sound like a pretty bad idea. Since you are using a LinearLayout, give your ListView layout_height="0dp" and layout_weight="1".

It's okay to let the ListView take the remainder of the screen. If it only has one row, it will show one row, no big deal. Unless you are trying to show something below the list, but what I've told you above should accomplish that.

Vicechancellor answered 18/3, 2013 at 15:11 Comment(2)
Thanks for your answer, but changing the height from "wrap_content" to 0dp/weight=1 results in same behaviour: the ListView fills the entire screen. In fact i was trying to show something below the list, but an entire listview showing just one element (when just one found) made me put the list below...Tonnie
You can still give it height=0 and weight=1. Add another view after it and the LinearLayout will allocate space for that item and give the remainder to the ListView. If instead you are trying to have an item that is fixed to the bottom of the list but scrolls with the content of the list, you want to look at ListView.addFooterView()Vicechancellor
A
0

If above doesn't work, you can just use RelativeLayout instead of linear and put the ListView underneath the EditText.

Arid answered 18/3, 2013 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.