0

I am creating some TextView, ImageView, HtmlView dynamically and adding it in following layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ad_space"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/navigation_bar"
        android:visibility="gone" >
    </LinearLayout>

    <ViewFlipper
        android:id="@+id/viewflipper"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ScrollView
            android:id="@+id/quiz_scroll_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/quizView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/answer_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
            </LinearLayout>
        </ScrollView>

        <ScrollView
            android:id="@+id/quiz_scroll_viewTwo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/quizViewTwo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/answer_layoutTwo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
            </LinearLayout>
        </ScrollView>
    </ViewFlipper>
</LinearLayout>

In QuizView I am adding some question related views at runtime, and for answers I am creating one list view runtime and adding to answer_layout. I am using BaseAdapter to create ListView runtime based on situation.

But my ListView is not showing all content, its showing only first cell.

I have adding ListView like this.

ListView ansList = new ListView(Test.this);
ansList.setAdapter(adapter);
ansList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ansCellLayout.addView(ansList);

it display only one list cell.

if I set some int value for height then its show all list contains.

ansList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 600));

But I cant Hardcode list view height as my contant is dynamic.

How to make list view as a WRAP_CONTENT in this scenario ?

nmw
  • 6,664
  • 3
  • 31
  • 32
Mac
  • 1,153
  • 2
  • 20
  • 34

2 Answers2

2

According to the Android developers as seen in this Google I/O 2010 video , the ListView's height cannot be set to wrap_content. And when it is , the first three child elements are considered for the height. The others are simply not included.

Try setting the ListView's height at runtime. Or even better set it to FILL_PARENT.

Aditya Kushwaha
  • 839
  • 6
  • 12
  • Yes I have tried with FILL_PARENT but it dosent work, I ma trying to set height runtime. – Mac Jan 08 '13 at 15:27
-1

From the documentation a ListView is

A view that shows items in a vertically scrolling list

So it's not supposed to be used with WRAP_CONTENT but rather to have a fixed height and scroll content inside it.

You'd better use a vertical LinearLayout for that purpose.

sdabet
  • 18,360
  • 11
  • 89
  • 158
  • I have used LinearLayout but problem with it is, i am adding different views at runtime like text view, Web view etc. I have faced lots of issue with webview like setting up Webview height, and i need to use two different listener onclick for textview , ontoch for webview, multiple web view schooling, so I choose listview, its solves lots of issues like scrolling,listener etc. – Mac Jan 07 '13 at 10:42
  • do you want the list to be scrollable or to wrap the content ? – sdabet Jan 07 '13 at 11:06
  • I want list view jsut display all his cells, at present its showing only one cell, if I set the height then its showing all cells, instead of setting height, it show take hight from his content. so I was using wrap the content – Mac Jan 07 '13 at 11:28
  • which means you don't need any scrolling on your list, right ? – sdabet Jan 07 '13 at 12:18
  • Yes, I dont need any scrolling for list. – Mac Jan 07 '13 at 12:54
  • then you should consider using a vertical `LinearLayout`, instead of a `ListView` – sdabet Jan 28 '13 at 11:03