Search code examples
androidlistviewandroid-activityandroid-scrollview

I want the entire activity to be scrollable but only listview is scrollable. How do I do that?


I pull data into my listview from servers. The listview is inside an activity. The activity has buttons above it.

When the data is received and when I scroll data, only listview is scrolled. The buttons remain fixed at the top.

I want the listview to take the required vertical height it needs so that I would need to scroll the activity instead of the listview.

My XML code :

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
            <TextView
                android:text="text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/view_c_name"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <TextView
                android:text="text4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view_c_name"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:id="@+id/view_c_motto" />

            <TextView
                android:text="text5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view_c_motto"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:id="@+id/view_c_details" />

            <TextView
                android:text="text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/view_c_address"
                android:layout_below="@+id/view_c_details"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:text="text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/view_c_act_text"
                android:layout_below="@+id/view_c_address"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="71dp" />

            <Button
                android:text="Button 1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:id="@+id/view_button"
                android:layout_below="@+id/view_c_address"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <Button
                android:text="button 2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/view_button"
                android:layout_toRightOf="@+id/view_button"
                android:layout_toEndOf="@+id/view_button"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:id="@+id/view_button2" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/view_c_act_text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/my_listview"
        android:scrollbars="none"
        />

</RelativeLayout>

Solution

  • The perfect code should be;

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <!--Inside the below layout your buttons will come. Modify it accordingly-->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <!--Your buttons will come here-->
    
            </LinearLayout>
    
            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    As shown above, Nested Scroll View should be used instead of Scroll View because there is a need of scrolling view (in this case List View) inside another scrolling view. The system is unable to decide which view to scroll and this is where Nested Scroll View comes in.