4

Here, I'm using a listView inside a ScrollView, everything is working fine but listView isn't displaying completely when I set its heightfill_parent. So I've fixed its height and it doesn't look good when I change the android phone device. Can anyone help me in it? I'm posting my xml class too.

Demo.xml

<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="25dp"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="6dp"
            android:text="@string/course_tracker"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_finishapp_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:weightSum="20" >

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/program_name" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical"
                android:background="#e9e9e9" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/colon"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="13"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@id/tv_program_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:orientation="horizontal"
            android:weightSum="20" >

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/program_description" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical"
                android:background="#e9e9e9" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/colon"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="13"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@id/tv_program_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:weightSum="20" >

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="10"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/course_name" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="10"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="@string/semester" />
            </LinearLayout>

            <TextView
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#E3E3E3"
                android:text="" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E3E3E3" />

        <ListView
            android:id="@id/list_course"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:layout_marginBottom="10dp" >
        </ListView>
    </LinearLayout>

 </LinearLayout></ScrollView>

Thanks.

  • 2
    You don't need to put the `ListView` in a `ScrollView`. The `ListView` is already scrollable as-is. – Marcus Feb 24 '15 at 05:52
  • why you want listview in scrollview,listview is already scrollable – Pramod Yadav Feb 24 '15 at 05:53
  • I know it. But I have to add some tables inside it and all data is coming from web service so I need to add `listView`. –  Feb 24 '15 at 05:54
  • I think what you want is http://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view – Arkar Aung Feb 24 '15 at 05:58
  • Sorry to say, your GUI design is much like web page design. This is not suitable for a common smart phone or a small tablet. The layout you have is barely manageable. For your sake or your project, you can separate the layouts onto separate files, more manageable. – The Original Android Feb 24 '15 at 07:51
  • I tried that too but no good result –  Feb 24 '15 at 08:40

2 Answers2

1

Dynamically set the height to ListView based on the list count,Like:

private void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem != null) {
            listItem.measure(
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        }

        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

call above method after setting the adapter to the ListView,Like:

urListView.setAdapter(urAdapter);
setListViewHeightBasedOnChildren(urListView);
user543
  • 3,623
  • 2
  • 16
  • 14
0

Just use the headerView of the ListView: Put all the things you want above the list in the headerView and all the things you want below in the footerView. The listView will be shown at full height and the headerView is scrollable.

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View,%20java.lang.Object,%20boolean)

http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View,%20java.lang.Object,%20boolean)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);

    mListViewHeaderView = initHeaderView(mListView, inflater);

    mListView = (ListView) view.findViewById(R.id.listView);
    mListView.addHeaderView(mListViewHeaderView, null, false);

    return view;
}


private View initHeaderView(ViewGroup container, LayoutInflater inflater) {
    View headerView = inflater.inflate(R.layout.fragment_atm_details_header, container, false);

    // configure header view

    return headerView;
}