Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a vertical scroll bar to Android layout when there 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

like image 566
deucalion0 Avatar asked May 15 '12 20:05

deucalion0


People also ask

How do I add a vertical scrollbar?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

Can scroll vertically android?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.


2 Answers

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>
like image 130
Izkata Avatar answered Oct 14 '22 05:10

Izkata


The first answer has got one mistake.

instead

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

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

use

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

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


 ......
  </ScrollView>
like image 41
Victor Pozdnyakov Avatar answered Oct 14 '22 04:10

Victor Pozdnyakov