Search code examples
androidandroid-layoutandroid-scrollview

adding widgets below a scrollview


I want to add few android widgets on the top, followed by a Scrollview and a few widgets below that Scrollview. Upon filling completely the Scrollview starts occupying the space allocated for the widgets(which were supposed to come after the Scrollview). Ultimately those widgets start appearing improperly.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:weightSum="10">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Enter Data"
        android:gravity="center_horizontal"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_weight="1"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="6">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:color/holo_green_light">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_green_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_blue_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_green_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_blue_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_green_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_blue_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_green_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_blue_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_green_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_blue_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="new Button"
                android:background="@android:color/holo_green_light"/>
        </LinearLayout>
    </ScrollView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Enter Data Again"
        android:gravity="center_horizontal"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit Again"
        android:layout_weight="1"/>

</LinearLayout>

In the following picture it can be seen that the Scollview uses the space allocated for widgets below it.


Solution

  • Change your layout code like below:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">
    
        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_horizontal"
            android:text="Enter Data" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text1"
            android:text="Submit" />
    
        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/text2"
            android:layout_below="@+id/button">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/holo_green_light"
                android:orientation="vertical">
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_green_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_green_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_green_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_green_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_green_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_light"
                    android:text="new Button" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_green_light"
                    android:text="new Button" />
            </LinearLayout>
        </ScrollView>
    
        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button2"
            android:gravity="center_horizontal"
            android:text="Enter Data Again" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Submit Again" />
    
    </RelativeLayout>
    

    View will be like:

    enter image description here