android horizontal scroll view
Asked Answered
A

2

9

i am beginner in android development and i am trying to create a horizontal scroll view that will contain different layouts.

the error that i am facing is : horozontal scroll view can only host one direct child. please advise thanks in advance

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tool"
        android:id="@+id/horizontalScrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

         <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:background="#ff0000">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#ff0000">

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#00ff00">

            </LinearLayout>

        </LinearLayout>

    </HorizontalScrollView>
Adna answered 15/10, 2012 at 17:21 Comment(7)
Can you define your question more precisely? How do you mean different layouts?Halverson
i will consider each layout as a viewAdna
The point is there is nothing wrong with this layout. Everything is properly encapsulated. Everything is within one LinearLayout, as it should be, which is in the HorizontalScrollView. No matter how many things you put in that one LinearLayout, they will scroll left/right.Halverson
can it be an emulator problem?Adna
Well, maybe, the emulator isn't perfect. The ScrollView layouts have effect only when their content is outside the borders of the screen. If everything fits on one screen, they just don't do anything. If you are seeing the error you wrote in the original question, try to restart your IDE. I'm not seeing that error, and I copied the code as is. It even runs on my phone, I've put some buttons.Halverson
thanks a lot man, i'll get a device as soon as possible, i really appreciate your helpAdna
Just mark any of the comments useful :).Halverson
S
11

Not only Horizontal but any vertical scrollview also will give this error. The error means that there should be only one child to a scrollview and that child can contain any number of sub childs.

So the bottom line is thios you should make only one direct child to scrollview and make your layout in that child only as

  <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tool"
    android:id="@+id/horizontalScrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

     <LinearLayout

        android:id="@+id/directchild"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="#ff0000">

    </LinearLayout>

</HorizontalScrollView>

Now create your desired layout within directchild layout.You will not get any error then

Sciomancy answered 15/10, 2012 at 17:27 Comment(2)
other layout are already childrens of this main layout and not childs of the scroll viewAdna
This isn't showing any error on my eclipse.If you want to see the effect give a width to different layouts and give fillparent width to the main layout,you will see the scrolling effect.Sciomancy
A
0

Hi this error is because scrollview can host only on direcxt child . take a relative or linear layout and write all the layouts inside that

Alston answered 21/7, 2020 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.