Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 699
John D. Avatar asked Mar 18 '13 14:03

John D.


1 Answers

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.

like image 165
Karakuri Avatar answered Oct 17 '22 17:10

Karakuri