Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android horizontal scroll view

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>
like image 228
mr. x Avatar asked Oct 15 '12 17:10

mr. x


People also ask

What is horizontal scroll view in Android?

HorizontalScrollView is used to scroll the child elements or views in a horizontal direction. HorizontalScrollView only supports horizontal scrolling. For vertical scroll, android uses ScrollView. Let's implement simple example of HorizontalScrollView.

How do I set horizontal scroll view?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In this above code we have declare Linear layout as parent and added Horizontal Scroll view.

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.


1 Answers

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

like image 63
Saurabh Avatar answered Oct 23 '22 05:10

Saurabh