Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A scroll view can have only one child Android XML file

So, I want to have a scrollview on my layout. I did this and got a scroll view can only have one child :

<?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:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="95dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:text="Latitude: "
        android:textSize="15dip" >

    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="unknown"
        android:textSize="15dip" >

    </TextView>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="90dip"
        android:layout_marginRight="5dip"
        android:text="Longitute: "
        android:textSize="15dip" >
    </TextView>

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unknown"
        android:textSize="15dip" >
    </TextView>
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="130dip"
    android:layout_height="30dip"
    android:layout_marginLeft="95dip"
    android:layout_marginTop="10dip"
    android:text="Find Coordinates"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextName"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="20dip"
    android:ems="10"
    android:hint="Enter Location Name"
    android:textSize="10dip" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editTextPhone"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="0dip"
    android:ems="10"
    android:hint="Enter Phone Number"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextEmail"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Email"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextDesc"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Description"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFlowers"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Flowers"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextPlants"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Plants"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFruit"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Fruit"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextVeggies"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Veggies"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextSun"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="115dip"
    android:ems="10"
    android:hint="Sunday"
    android:textSize="10dip" />

<Button
    android:id="@+id/saveLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="90dip"
    android:layout_marginTop="10dip"
    android:text="Save Location" />

<Button
    android:id="@+id/seeAll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="120dip"
    android:text="See all" />

</ScrollView> 

Then, following the answer to this question

java.lang.IllegalStateException: ScrollView can host only one direct child

I did this:

<?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="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="95dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:text="Latitude: "
        android:textSize="15dip" >

    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="unknown"
        android:textSize="15dip" >

    </TextView>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation = "vertical" >

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="90dip"
        android:layout_marginRight="5dip"
        android:text="Longitute: "
        android:textSize="15dip" >
    </TextView>

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unknown"
        android:textSize="15dip" >
    </TextView>
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="130dip"
    android:layout_height="30dip"
    android:layout_marginLeft="95dip"
    android:layout_marginTop="10dip"
    android:text="Find Coordinates"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextName"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="20dip"
    android:ems="10"
    android:hint="Enter Location Name"
    android:textSize="10dip" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editTextPhone"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="0dip"
    android:ems="10"
    android:hint="Enter Phone Number"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextEmail"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Email"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextDesc"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Description"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFlowers"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Flowers"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextPlants"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Plants"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFruit"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Fruit"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextVeggies"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Veggies"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextSun"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="115dip"
    android:ems="10"
    android:hint="Sunday"
    android:textSize="10dip" />

<Button
    android:id="@+id/saveLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="90dip"
    android:layout_marginTop="10dip"
    android:text="Save Location" />

<Button
    android:id="@+id/seeAll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="120dip"
    android:text="See all" />

</ScrollView> 

but still no luck. Any ideas? Thanks.

like image 658
nick Avatar asked May 23 '14 02:05

nick


People also ask

Which view can hold one direct child?

The ScrollView can contain only one child view or ViewGroup, which normally is a LinearLayout.

What does scroll view do?

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

The key here is

ScrollView can host only one direct child

"direct" being the keyword. So you can have

<ScrollView
    ...>
    <LinearLayout>
       ....
       <!-- you can have as many Views in here as you want -->
    </LinearLayout>
</ScrollView>

So, the LinearLayout in the above example would be your one direct child. And that child can have as many children as you want. Which means you can have all of your other LinearLayouts inside that first one. But you can't have more than one on the same hierarchical level.

like image 135
codeMagic Avatar answered Sep 30 '22 17:09

codeMagic