-1

Is it possible to show listview and scroll view together in the xml.This is my xml file. I have defined a listview outside the scrollview. The problem is when i scroll the view in my device the list view is not visible. And if the view is small as if i am not scrolling then the listview is visible. I want a scroll view below which i want a listview to show list data. Help me out if any one knows the solution or the mistake.

Code

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ScrollView
            android:id="@+id/scrollViewAgenda"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relheader" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <View
                    android:id="@+id/viewUpper"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray_bg" />

                <RelativeLayout
                    android:id="@+id/relEventDesc"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/txtDescriptionAgenda"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="20dp"
                        android:paddingLeft="10dp"
                        android:text="Description"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                    <TextView
                        android:id="@+id/txtAgendaDescription"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_below="@+id/txtDescriptionAgenda"
                        android:layout_marginTop="5dp"
                        android:padding="@dimen/eventInformationTrDescriptionPadding"
                        android:textColor="@color/gray_font"
                        android:textSize="@dimen/eventInformationTextSize" />
                </RelativeLayout>

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/txtDescription"
                    android:layout_below="@+id/trDescription"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="10dp" >

                    <TextView
                        android:id="@+id/txtExpandDescriptionAgenda"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:gravity="center"
                        android:text="Read more"
                        android:textColor="#00E5EE"
                        android:textSize="15sp" />
                </TableRow>

                <View
                    android:id="@+id/viewDown"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray_bg" />

                <TableRow
                    android:id="@+id/tabRowispckerIconAndHeaderInfo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/txtSpeakerLable"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:text="Speaker"
                        android:textSize="18sp" />
                </TableRow>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/linLayNoDataFound"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:visibility="gone" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:text="No Speaker Defined."
                            android:textColor="@color/gray"
                            android:textSize="17sp" />

                        <View
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            android:background="@drawable/no_data" />
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

    <ListView
        android:id="@+id/listViewAgenda"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#FFFFFF" >
    </ListView>

</LinearLayout>

If any one aware of such situation please help me out.

Savin Sharma
  • 769
  • 5
  • 22

2 Answers2

0

Thats because the ScrollView overlaps your ListViewand hide it. Use a LinearLayout as root view and the layout_weight to share the screen height.

Eg:

<LinearLayout 
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3" >

        <ScrollView
            android:id="@+id/scrollViewAgenda"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relheader" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <View
                    android:id="@+id/viewUpper"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray_bg" />

                <RelativeLayout
                    android:id="@+id/relEventDesc"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/txtDescriptionAgenda"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="20dp"
                        android:paddingLeft="10dp"
                        android:text="Description"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                    <TextView
                        android:id="@+id/txtAgendaDescription"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_below="@+id/txtDescriptionAgenda"
                        android:layout_marginTop="5dp"
                        android:padding="@dimen/eventInformationTrDescriptionPadding"
                        android:textColor="@color/gray_font"
                        android:textSize="@dimen/eventInformationTextSize" />
                </RelativeLayout>

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/txtDescription"
                    android:layout_below="@+id/trDescription"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="10dp" >

                    <TextView
                        android:id="@+id/txtExpandDescriptionAgenda"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:gravity="center"
                        android:text="Read more"
                        android:textColor="#00E5EE"
                        android:textSize="15sp" />
                </TableRow>

                <View
                    android:id="@+id/viewDown"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray_bg" />

                <TableRow
                    android:id="@+id/tabRowispckerIconAndHeaderInfo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/txtSpeakerLable"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:text="Speaker"
                        android:textSize="18sp" />
                </TableRow>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/linLayNoDataFound"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:visibility="gone" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:text="No Speaker Defined."
                            android:textColor="@color/gray"
                            android:textSize="17sp" />

                        <View
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            android:background="@drawable/no_data" />
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

    <ListView
        android:id="@+id/listViewAgenda"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:divider="#FFFFFF"
        android:layout_weight="0.7" >
    </ListView>

</LinearLayout>
Rami
  • 7,879
  • 12
  • 36
  • 66
0

Use This

public class ExpandableHeightListView
{
    public static void setListViewHeightBasedOnChildren(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {

            View listItem = listAdapter.getView(i, null, listView);

            listItem.measure(0, 0);

            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

        listView.setLayoutParams(params);

    }
}

In MainActivity pass your listview object in this method.

setListViewHeightBasedOnChildren(listView);

Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45