-2

I am trying very hard to understand the ScrollView and Linearlayout and constantly trying to use inside my activity but I"m getting this error..I already have only one Linearlyout inside scroll view and other child are wrapped in wrap content both height and width but still the app crashes..Please help

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="#ffffff"
    tools:context=".Blood">

        <TextView
           android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:textStyle="bold"
            android:background="#f4051d"
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Blood Type: Facts and Information"/>


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:padding="5dp">

<TextView
    android:id="@+id/intro"
   android:layout_width="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="#000000"
    android:fontFamily="sans-serif-condensed"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="When "/>


    <TextView
       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Blood Types ( Groups )"/>

    <TextView
        android:id="@+id/blood_types"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="A blood "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image_one"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_blood_one"/>

    <TextView
        android:id="@+id/blood_type_image"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="In addition ."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Donating Blood By Compatible Type"/>


    <TextView
        android:id="@+id/donate"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="Blood "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <ImageView
        android:id="@+id/image_two"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_blood_two"/>


    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Finding Out Your  Blood Type"/>


    <TextView
        android:id="@+id/find_blood_type"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="It is easy "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/image_three"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_blood_three"/>

    <TextView
        android:id="@+id/find_blood_image"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="In most"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="What Does Blood Group RH Factor Mean"/>


    <TextView
        android:id="@+id/rh_factor"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="RH factor ."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />





    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="What is the Rarest Blood Type"/>


    <TextView
        android:id="@+id/rare_blood"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="According "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Blood Type Diet"/>


    <TextView
        android:id="@+id/blood_type_diet"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text=" The Ea"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />




    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Blood Types and Personality"/>


    <TextView
        android:id="@+id/personality"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="Legend"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    

    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Facts: Blood Types"/>


    <TextView
        android:id="@+id/facts"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="Blood"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    </ScrollView>
</LinearLayout>

Thanks in advance.

Bir Nepali
  • 429
  • 1
  • 8
  • 20
  • As the error states clearly:- scrollview can host only one direct child. Put everything you have inside scrollview in a single linear or realtive layout – Vivek Mishra Sep 08 '18 at 13:19
  • 1
    Possible duplicate of [How can I avoid "IllegalStateException: Scrollview can host only one direct child"?](https://stackoverflow.com/questions/3735095/how-can-i-avoid-illegalstateexception-scrollview-can-host-only-one-direct-chil) – Vivek Mishra Sep 08 '18 at 13:19
  • you need to include only one child in scrollView – Moustafa EL-Saghier Sep 08 '18 at 13:21

2 Answers2

1

You need to have only one child in ScrollView like 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="match_parent">

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

    <ImageView ... />
    <TextView ... />
    <TextView ... />
    <TextView ... />

</LinearLayout>
</ScrollView>
Anubhav Gupta
  • 2,492
  • 14
  • 27
0

No, you have one SrollView inside a LinearLayout. Your ScrollView just has a bunch of TextViews in it. Change it to this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        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:orientation="vertical"
        android:background="#ffffff"
        tools:context=".Blood">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="56dp"
        android:textStyle="bold"
        android:background="#f4051d"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blood Type: Facts and Information"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff"
        android:padding="5dp">

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

            <TextView
                android:id="@+id/intro"
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#000000"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="When "/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Blood Types ( Groups )"/>

            <TextView
                android:id="@+id/blood_types"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="A blood "
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/image_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawable_blood_one"/>

            <TextView
                android:id="@+id/blood_type_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="In addition ."
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Donating Blood By Compatible Type"/>

            <TextView
                android:id="@+id/donate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="Blood "
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/image_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawable_blood_two"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Finding Out Your  Blood Type"/>

            <TextView
                android:id="@+id/find_blood_type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="It is easy "
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/image_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawable_blood_three"/>

            <TextView
                android:id="@+id/find_blood_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="In most"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="What Does Blood Group RH Factor Mean"/>

            <TextView
                android:id="@+id/rh_factor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="RH factor ."
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="What is the Rarest Blood Type"/>

            <TextView
                android:id="@+id/rare_blood"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="According "
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Blood Type Diet"/>

            <TextView
                android:id="@+id/blood_type_diet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text=" The Ea"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Blood Types and Personality"/>

            <TextView
                android:id="@+id/personality"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="Legend"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#0e06f2"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Facts: Blood Types"/>

            <TextView
                android:id="@+id/facts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="Blood"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />

        </LinearLayout>

    </ScrollView>
</LinearLayout>
TheWanderer
  • 16,775
  • 6
  • 49
  • 63