Sunil
Sunil

Reputation: 49

Android ScrollView is not working saying Scrollview cannot host multiple childs

Hi In my android application I have multiple linear layouts and edit texts in them In my layout file I have 5 other linear layouts and I should be able to scroll that page,but I'm facing a problem, tried many solutions but its not working.

This is what I have tried.

<?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" >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Team :"
        android:textColor="#000000"
        android:textSize="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>
    <Spinner
        android:id="@+id/TeamSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"></Spinner>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1)"
        android:textColor="#000000"
        android:textSize="20dp"
        android:layout_marginTop="25dp"/>
    <EditText
        android:id="@+id/first1"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:hint="FirstName"
        android:background="@drawable/green_border"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="30dp"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/last1"
        android:inputType="numberDecimal"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/green_border"
        android:textSize="20dp"
        android:hint="LastName"
        android:textColor="#000000"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/email1"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="Email"
        android:textColor="#000000"
        android:textSize="20dp" ></EditText>
    <EditText
        android:id="@+id/mobile1"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="MobileNo"
        android:inputType="numberDecimal"
        android:textColor="#000000"
        android:textSize="20dp" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/darker_gray"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/number2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2)"
        android:textColor="#000000"
        android:textSize="20dp"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/first2"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:hint="FirstName"
        android:background="@drawable/green_border"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/last2"
        android:inputType="numberDecimal"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/green_border"
        android:textSize="20dp"
        android:hint="LastName"
        android:textColor="#000000"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/email2"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="Email"
        android:textColor="#000000"
        android:textSize="20dp" ></EditText>
    <EditText
        android:id="@+id/mobile2"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="MobileNo"
        android:inputType="numberDecimal"
        android:textColor="#000000"
        android:textSize="20dp" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/darker_gray"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/number3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3)"
        android:textColor="#000000"
        android:textSize="20dp"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/first3"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:hint="FirstName"
        android:background="@drawable/green_border"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/last3"
        android:inputType="numberDecimal"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/green_border"
        android:textSize="20dp"
        android:hint="LastName"
        android:textColor="#000000"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/email3"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="Email"
        android:textColor="#000000"
        android:textSize="20dp" ></EditText>
    <EditText
        android:id="@+id/mobile3"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="MobileNo"
        android:inputType="numberDecimal"
        android:textColor="#000000"
        android:textSize="20dp" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/darker_gray"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/number4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4)"
        android:textColor="#000000"
        android:textSize="20dp"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/first4"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:hint="FirstName"
        android:background="@drawable/green_border"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/last4"
        android:inputType="numberDecimal"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/green_border"
        android:textSize="20dp"
        android:hint="LastName"
        android:textColor="#000000"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/email4"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="Email"
        android:textColor="#000000"
        android:textSize="20dp" ></EditText>
    <EditText
        android:id="@+id/mobile4"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="MobileNo"
        android:inputType="numberDecimal"
        android:textColor="#000000"
        android:textSize="20dp" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/darker_gray"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/number5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5)"
        android:textColor="#000000"
        android:textSize="20dp"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/first5"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:hint="FirstName"
        android:background="@drawable/green_border"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/last5"
        android:inputType="numberDecimal"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/green_border"
        android:textSize="20dp"
        android:hint="LastName"
        android:textColor="#000000"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/email5"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="Email"
        android:textColor="#000000"
        android:textSize="20dp" ></EditText>
    <EditText
        android:id="@+id/mobile5"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_border"
        android:hint="MobileNo"
        android:inputType="numberDecimal"
        android:textColor="#000000"
        android:textSize="20dp" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/darker_gray"/>
</LinearLayout>

Upvotes: 0

Views: 71

Answers (3)

Avi Levin
Avi Levin

Reputation: 1868

You need to set a container layout which will be the father of all your linear layouts.

It looks something like this (The container can be Relative or Linear):

<ScrollView
    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:fillViewport="true">


    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    (all your view elements)

    </RelativeLayout>
</ScrollView>

Upvotes: 3

TommySM
TommySM

Reputation: 3883

The reason this is happening is because Scroll View needs to have a single child, try this (also works with a vertical linear layout):

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/grey"
        >

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
<!-- put the other stuff in here -->

    </RelativeLayout>
</ScrollView>

I suggest also toread about it a little bit, you might find some more usefull info.

Upvotes: 1

roiberg
roiberg

Reputation: 13747

Thats because you have a lot of LinearLayouts inside of your scrollView; You can have only one child (This child can have a lot of Childs of his own).
Try adding one LineartLayout with vertical orientation under the scrollView that will wrap all of the Childs.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- put all of the other layouts here -->

</LinearLayout>
</ScrollView>

Upvotes: 2

Related Questions