Search code examples
androidandroid-layoutscrollviewvertical-scrolling

How to add a vertical scroll bar to Android layout with many children in the layout?


I have been trying to figure out how to ensure the screen can be scrolled vertically as there are many radio buttons and they don't fit on the screen. I have tried most of the solutions posted on Stackoverflow, but I keep getting errors, here is my layout code at my last attempt:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

 <RadioGroup
        android:id="@+id/radioSharing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioSharingYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_sharing_yes"  />

    <RadioButton
        android:id="@+id/radioSharingNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_sharing_no" />

</RadioGroup>

 <RadioGroup
    android:id="@+id/radioInternet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioInternetYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_internet_yes"  />

    <RadioButton
        android:id="@+id/radioInternetNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_internet_no" />

</RadioGroup>

<RadioGroup
    android:id="@+id/radioMap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMapYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_map_yes"  />

    <RadioButton
        android:id="@+id/radioMapNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_map_no" />

</RadioGroup>

    <RadioGroup
    android:id="@+id/radioCalling"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioCallingYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_calling_yes"  />

    <RadioButton
        android:id="@+id/radioCallingNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_calling_no" />

</RadioGroup>

<RadioGroup
    android:id="@+id/radioDatabase"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioDatabaseYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_database_yes"  />

    <RadioButton
        android:id="@+id/radioDatabaseNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_database_no" />

</RadioGroup>

<Button
    android:id="@+id/btnSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="@string/btn_display" />


</LinearLayout>
</ScrollView>

The error I am getting is : "ScrollView can host only one direct child"

Any advice would be appreciated, Thanks


Solution

  • Take a closer look at your nesting:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
     <RadioGroup
     ......
     </RadioGroup>
    
    </LinearLayout>
    </ScrollView>
    

    You should have LinearLayout entirely within ScrollView, like so:

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
     <RadioGroup
     ......
     </RadioGroup>
    
    </LinearLayout>
    </ScrollView>