10

I have a problem with the ListView in Android. When I set android:layout_height="wrap_content", ListView stays just one or two rows long and I can't find a way to make him "wrapped".

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:id="@+id/question" 
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content" />
            <ListView android:id="@+id/ListView01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

Is there any way to accomplish this? Thanks in advance!

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
Czechnology
  • 14,832
  • 10
  • 62
  • 88
  • is there any particular reason why you using so many "wrappers" around your ListView? – Tivie Nov 29 '10 at 01:18
  • Well, ScrollView can hold only one child, so there's LinearLayout to hold TextView and ListView. The FrameLayout might be unnecessary. So is there a better way to reproduce the ListView look? I've seen some ideas with populating a LinearLayout with list items but the description was not very "descriptive". – Czechnology Nov 29 '10 at 02:11

3 Answers3

30

You should never set the height of a ListView to wrap_content. Either set the height to fill_parent or do something like this:

<ListView
    android:layout_height="0dp"
    android:layout_weight="1"
    />

Source: http://www.youtube.com/watch?v=wDBM6wVEO70&t=40m45s

bmaupin
  • 14,427
  • 5
  • 89
  • 94
10

It appears you're trying to add a header to your listview. Listview has built-in support for headers (and footers). The method for setting a header is listview.addHeader(View view). You can customize the header as needed.

miniBill
  • 1,743
  • 17
  • 41
Patrick Jackson
  • 18,766
  • 22
  • 81
  • 141
  • 1
    This is manifestly not the answer to the question asked. The actual question wants to know how to make the ListView stretch in height to match the number of items it contains. – Phil Wright Sep 14 '17 at 05:02
2

use ExpandableHeightGridView.java of gitHub ExpandableHeightGridView Try for scrollview

Arth Tilva
  • 2,496
  • 22
  • 40