0

I have a problem with scroll view not working with image view for some reason. Here is the code. What is wrong with code code? And why does it giving me this error "ScrollView can host only one direct child"?

Caused by: android.view.InflateException: Binary XML file line #34: ScrollView can host only one direct child Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child at android.widget.ScrollView.addView(ScrollView.java:476) at android.view.LayoutInflater.rInflate(LayoutInflater.java:867) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompa

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/click_me"
       android:layout_marginBottom="10dp"
       android:id="@+id/button" />




    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:hint="@string/fetch_text_here"
            android:padding="10dp"
            android:textSize="24sp"
            android:id="@+id/fetchdata"
            />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/ic_launcher_background"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="130dp"
            tools:ignore="VectorDrawableCompat" />

    </ScrollView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/closeMe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="close me"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0.95"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.938" />



    </androidx.constraintlayout.widget.ConstraintLayout>


</RelativeLayout>
Til
  • 5,150
  • 13
  • 26
  • 34
Thien Vu
  • 1
  • 5

1 Answers1

0

The ScrollView view only accept one child, if you need more than one, then you can use a LinearLayout like the only child to the ScrollView, and in the LinearLayout you can have the 2 Childs or more.

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"> 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/fetch_text_here"
                android:padding="10dp"
                android:textSize="24sp"
                android:id="@+id/fetchdata"/>
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/ic_launcher_background"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="130dp"
                tools:ignore="VectorDrawableCompat" />
        </LinearLayout>

    </ScrollView>