1

I want to have a scrollable list which also includes list view. I did research everywhere and no solution yet :( As per the code below, when I add ScrollView, the listview height becomes smaller (single row). I want the ListView height to be dynamic (based on the content) and also make the complete page scrollable. How do I achieve this? Or is there a way to create dynamic linear / relative list UI? Please suggest.

XML code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_horizontal_margin"
    android:orientation="vertical" >

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin">

        <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/kollege_name"
        android:width="170dp" />

        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="right"
        android:text="@string/grade_value"
        android:width="120dp" />

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="@string/code_value" />
        </RelativeLayout>

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />


        <TextView
        android:id="@+id/textView4"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/your_fit"
        android:textSize="12sp" />

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

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tuition"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"

            android:text="@string/tuition_value" />

        </RelativeLayout>


        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placements"
        android:textSize="12sp" />

        <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView12"
        android:text="@string/EXTC"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView13"
        android:text="@string/IT"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView14"
        android:text="@string/COMP"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView15"
        android:text="@string/MECH"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView16"
        android:text="@string/INST"
        android:layout_marginTop="3dp" />
        </RelativeLayout>

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />


        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
        android:id="@+id/textView18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placements"
        android:textSize="12sp" />

        <TextView
        android:id="@+id/textView19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView18"
        android:text="@string/EXTC"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView19"
        android:text="@string/IT"
        android:layout_marginTop="3dp" />
        </RelativeLayout>
        </LinearLayout>

        </ScrollView>

</LinearLayout>
1
  • I'm pretty sure you don't have to wrap a listView within a scrollView. Commented Sep 18, 2013 at 3:26

3 Answers 3

11

It's better to set diffrent view type inside the ListView Adapter or use the ListView Footer and Header.

Also you can use this Method which will fix the issue you are facing with ListView in ScrollView

public static void updateListViewHeight(ListView myListView) {
     ListAdapter myListAdapter = myListView.getAdapter();
     if (myListAdapter == null) {            
              return;
     }
    //get listview height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount()
    for (int size = 0; size < adapterCount ; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView 
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This script solved the issue. By the way, does this script work on all android API's or is it specific to some? I need to make the app compatible from SDK-10 onwards..
it should work fine , but keep in mind that the ListView will be fixed viewd always and fully loaded on setting the adapter.
this is not working with listviews which some rows are different. (E.g: i have a listview, in adapter, i change row layout for first row. For my lw, this method does not work properly)
Yes, I also need a solution when a list-item has multiple line text in its TextView. Need a solution badly.
2

You should not use listview with in scroll view.Because each having a own scrolling.I am also suffer this problem.If you want to set both in your layout just you set listview weight.

1 Comment

Hi Satheesh, I am fairly new to Android. Can u pls help me with the solution in the XML file? Thanks...
2

You should not put listview within scrollview. Have your listview oustide the scrollview or add your other views as a header and footer to listview.

Quoting from the docs

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

You can add your views as header and footer to your listview. The header and footer will scroll.

ListView without scroll on Android

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.