Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView distrubing ListView inside of LinearLayout

I have LinearLayout whcih contains Buttons and ListViews. After getting many buttons and I decide to put that LinearLayout into ScrollView but after putting ScrollView my ListViews are showing only one Item.

Following is the code before ScrollView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".WorkplaceObservationActivity" >

    <Button
        android:id="@+id/btn_reportType"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Report Type" />

    <ListView
        android:id="@+id/lv_reportType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" >
    </ListView>

    <Button
        android:id="@+id/btn_defineJob"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Define Job" />

    <ListView
        android:id="@+id/lv_Jobs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:visibility="gone" >
    </ListView>

    <Button
        android:id="@+id/btn_timeLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time and Location" />

    <LinearLayout
        android:id="@+id/ll_timeLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <Button
            android:id="@+id/btn_setCurrentTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Use current time and date" />

        <Button
            android:id="@+id/btn_setCustomTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Enter custom time" />

        <Button
            android:id="@+id/btn_setCustomDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Enter custom date" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Time and Date: "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:visibility="gone" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_ReportDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Report Details" />

    <LinearLayout
        android:id="@+id/ll_ReportDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <Button
            android:id="@+id/btn_CaptureVideo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Capture Video" />

        <ListView
            android:id="@+id/lv_ReportDetailsItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
        </ListView>
    </LinearLayout>

    <Button
        android:id="@+id/btn_Review"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Review" />

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

        <TextView
            android:id="@+id/tv_Review_ReportType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Report Type: "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/tv_Review_DateTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date and Time: "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/tv_Review_SupportingFiles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Supporting Files: "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ListView
            android:id="@+id/lv_ReviewItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" >
        </ListView>

        <Button
            android:id="@+id/btn_Review_Send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/btn_Review_Save"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Save Draft" />

            <Button
                android:id="@+id/btn_Review_Discard"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Discard" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

for Above Code I am getting Following Screen Shot enter image description here

Following is the xml code after ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".WorkplaceObservationActivity" >

        <Button
            android:id="@+id/btn_reportType"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Report Type" />

        <ListView
            android:id="@+id/lv_reportType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" >
        </ListView>

        <Button
            android:id="@+id/btn_defineJob"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Define Job" />

        <ListView
            android:id="@+id/lv_Jobs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:visibility="gone" >
        </ListView>

        <Button
            android:id="@+id/btn_timeLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Time and Location" />

        <LinearLayout
            android:id="@+id/ll_timeLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone" >

            <Button
                android:id="@+id/btn_setCurrentTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="Use current time and date" />

            <Button
                android:id="@+id/btn_setCustomTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="Enter custom time" />

            <Button
                android:id="@+id/btn_setCustomDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="Enter custom date" />

            <TextView
                android:id="@+id/tv_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Time and Date: "
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:visibility="gone" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_ReportDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Report Details" />

        <LinearLayout
            android:id="@+id/ll_ReportDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone" >

            <Button
                android:id="@+id/btn_CaptureVideo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Capture Video" />

            <ListView
                android:id="@+id/lv_ReportDetailsItems"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" >
            </ListView>
        </LinearLayout>

        <Button
            android:id="@+id/btn_Review"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Review" />

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

            <TextView
                android:id="@+id/tv_Review_ReportType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Report Type: "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tv_Review_DateTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date and Time: "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tv_Review_SupportingFiles"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Supporting Files: "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <ListView
                android:id="@+id/lv_ReviewItems"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="gone" >
            </ListView>

            <Button
                android:id="@+id/btn_Review_Send"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Send" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2" >

                <Button
                    android:id="@+id/btn_Review_Save"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Save Draft" />

                <Button
                    android:id="@+id/btn_Review_Discard"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Discard" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</ScrollView>

and following is the sceen shot of above

enter image description here

I want to show complete list of items after scrollview.

like image 692
Ahmed Nawaz Avatar asked Dec 08 '22 15:12

Ahmed Nawaz


1 Answers

There is a conflict between the ScrollView and the ListView. In general, you cannot put a ListView inside a ScrollView. ListView already understands about scrolling and will scroll itself. ListView needs to know how much vertical space on the screen it should use, so that it can control the scrolling and display of its children. It is difficult to understand how the layout that you've posted should behave.

In any case, the only way to put a ListView inside a ScrollView is to tell the ListView exactly how much vertical space it should use. You cannot use match_parent or wrap_content for layout_height, you need to fix the vertical size. This helps the ScrollView to determine how much vertical space each of its children takes up, and then the ScrollView can do the right thing.

Try setting android:layout_height="80dp" on all of your ListViews to see how this behaves.

EDIT: After looking at the layout again, I have another suggestion

After looking at the layout again, I am assuming that you want to have only one of the ListViews visible at a given time (the others would all be GONE). In this case, you might try the following:

  1. Remove the surrounding ScrollView
  2. Set android:layout_weight="1" and android:layout_height="1px" on all ListViews

This basically tells the layout manager that the ListView should be given all available space on the screen (ie: whatever is left over after the other Views are laid out). This will make the ListView expand to take up all available space on the screen, while keeping all the other Views on screen and the entire screen will not scroll, only the ListView. This may be a better way to give you what you want.

You might also consider using ExpandableListView, which seems to be what you are trying to recreate here.

like image 68
David Wasser Avatar answered Jan 08 '23 08:01

David Wasser