[android] Scrollview can host only one direct child

I have multiple LinearLayouts with a combined height that easily exceeds a device's screen height. So in order to make my layout scrollable, I tried adding in a ScrollView, but unfortunately I get the following error:

Scrollview can host only one direct child

I did some research, but couldn't really make sense of how to solve it, even after reading a couple of solutions. I tried a few changes, but nothing worked for me so far. Any help?

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

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

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="20dip">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 1"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q1_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 2"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q2_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 3"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q3_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 4"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q4_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 5"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q5_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 6"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q6_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 7"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q7_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 8"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q8_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 9"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q9_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 10"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q10_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 11"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q11_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 12"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q12_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 13"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q13_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 14"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q14_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 15"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q15_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 16"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q16_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 17"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q17_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 18"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q18_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 19"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q19_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>
</LinearLayout>
        <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Question 20"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/q20_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        android:gravity="center"/>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Score:"
        android:paddingBottom="20dip"
        android:gravity="center" />
    <TextView
        android:id="@+id/final_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0/5"
        android:gravity="center"/>
</LinearLayout>
    </LinearLayout>
        </ScrollView>

This question is related to android android-layout android-scrollview

The answer is


Wrap all the children inside of another LinearLayout with wrap_content for both the width and the height as well as the vertical orientation.


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to android-scrollview

Android - how to make a scrollable constraintlayout? Android: ScrollView vs NestedScrollView Recyclerview inside ScrollView not scrolling smoothly RecyclerView inside ScrollView is not working Android list view inside a scroll view Scrollview can host only one direct child Detect end of ScrollView Page scroll when soft keyboard popped up Can I scroll a ScrollView programmatically in Android? Disable ScrollView Programmatically?