Question

I want to add the scrollview in my application. The application contains two linear layouts in which I add data dynamically. I want to scroll all the screen, so it shows all the data. When I try to add it shows error,"Scroll view can have only one child". How to add Scrollview in the layout.xml?

Here is my Layout.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/myapp"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/my_logo"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:src="@drawable/my_logo" 
        >
    </ImageView>

    <TextView
        android:id="@+id/label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label1"
        android:layout_below="@id/my_logo" />

   <Spinner 
       android:id="@+id/spinner1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/label1"/>

   <TextView
        android:id="@+id/label2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner1"
        android:text="@string/label2" />

   <Spinner 
       android:id="@+id/spinner2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/label2"/>

   <TextView
        android:id="@+id/label3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner2"
        android:text="@string/label3" />


   <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/list"
        android:layout_below="@id/label3"
        >

    </LinearLayout>

   <Button 
       android:id="@+id/submit_button"
       android:layout_below="@id/list2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/submit_button"/>

   <TextView
        android:id="@+id/label4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/submit_button"
        android:text="@string/label4" />

    <LinearLayout 
    android:id="@+id/my_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_below="@id/label4" />

</RelativeLayout>
Was it helpful?

Solution

The ScrollView is very similar to FrameLayout. They both tend to work better when there is only one view in there. Make the ScrollView the parent View and place everything inside of there.

OTHER TIPS

Like it says, a scrollview can have only one child, just put a layout as a child and all your content in this layout.

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myapp" >
    <ImageView
        android:id="@+id/my_logo"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:src="@drawable/my_logo" 
        >
    </ImageView>

    <TextView
        android:id="@+id/label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label1"
        android:layout_below="@id/my_logo" />

   <Spinner 
       android:id="@+id/spinner1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/label1"/>

   <TextView
        android:id="@+id/label2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner1"
        android:text="@string/label2" />

   <Spinner 
       android:id="@+id/spinner2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/label2"/>

   <TextView
        android:id="@+id/label3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner2"
        android:text="@string/label3" />


   <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/list"
        android:layout_below="@id/label3"
        >

    </LinearLayout>

   <Button 
       android:id="@+id/submit_button"
       android:layout_below="@id/list2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/submit_button"/>

   <TextView
        android:id="@+id/label4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/submit_button"
        android:text="@string/label4" />

    <LinearLayout 
    android:id="@+id/my_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_below="@id/label4" />
</RelativeLayout>
</ScrollView>

Just put Relative layout inside scrollview it will do the trick

<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/myapp"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/my_logo"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        >
    </ImageView>

    <TextView
        android:id="@+id/label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ss"
        android:layout_below="@id/my_logo" />

   <Spinner 
       android:id="@+id/spinner1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/label1"/>

   <TextView
        android:id="@+id/label2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner1"
        android:text="ss" />

   <Spinner 
       android:id="@+id/spinner2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/label2"/>

   <TextView
        android:id="@+id/label3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner2"
        android:text="ss" />


   <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/list"
        android:layout_below="@id/label3"
        >

    </LinearLayout>

   <Button 
       android:id="@+id/submit_button"
       android:layout_below="@id/list"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="sss"/>

   <TextView
        android:id="@+id/label4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/submit_button"
        android:text="ssss" />

    <LinearLayout 
    android:id="@+id/my_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_below="@id/label4" />

</RelativeLayout>
</ScrollView>

Wrap whole Layout inside Relative Layout or FrameLayout And then add ScrollView ScrollView can have only one direct element as child

<ScrollView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
<!--         yourLayout here -->

    </RelativeLayout>
</ScrollView
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top