Home > Back-end >  Scroll View making an error on Android Studio
Scroll View making an error on Android Studio

Time:12-24

I'm trying to make a layout resource file. however, in the process, I decided that I wanted the page to be in scroll view so I can add more items. when I added the scroll view tag and closing tag the layout resource file stopped responding and it makes an error, and the app won't open on the emulator because of it.

Here's the XML code:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="@color/generalBackground"
    android:orientation="vertical"
    android:padding="5dp"
    android:weightSum="100">

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


        <!-- Book Info -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal"
            android:weightSum="100">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="40" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="60"
                android:orientation="vertical"
                android:weightSum="60">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="20"
                    android:gravity="center"
                    android:text="Book Title"
                    android:textSize="25sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:gravity="center"
                    android:text="Author"
                    android:textSize="17sp"
                    android:textStyle="italic" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:text="Geners"
                    android:textSize="17sp"
                    android:textStyle="italic" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:text="Pages"
                    android:textSize="17sp"
                    android:textStyle="italic" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:text="Language"
                    android:textSize="17sp"
                    android:textStyle="italic" />

            </LinearLayout>
        </LinearLayout>

        <!-- Ratings -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:orientation="horizontal">
            <!-- Good Reads -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="15dp"
                android:layout_weight="10"
                android:gravity="center"
                android:orientation="vertical"
                android:weightSum="20">

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:background="@drawable/goodreads" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:orientation="horizontal"
                    android:weightSum="30">

                    <TextView
                        android:id="@ id/goodreadsr"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text=""
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="/"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="5"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>

            <!-- Common Sense Media -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="15dp"
                android:layout_weight="10"
                android:gravity="center"
                android:orientation="vertical"
                android:weightSum="20">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:background="@drawable/commonsensemedia" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:orientation="horizontal"
                    android:weightSum="30">

                    <TextView
                        android:id="@ id/commonsensemediar"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text=""
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="/"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="5"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>


            <!-- Read Print -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="15dp"
                android:layout_weight="10"
                android:gravity="center"
                android:orientation="vertical"
                android:weightSum="20">

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:background="@drawable/readprint" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:orientation="horizontal"
                    android:weightSum="30">

                    <TextView
                        android:id="@ id/readprintr"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text=""
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="/"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="5"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>

            <!-- Kindle -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="15dp"
                android:layout_weight="10"
                android:gravity="center"
                android:orientation="vertical"
                android:weightSum="20">

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:background="@drawable/kindle" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10"
                    android:orientation="horizontal"
                    android:weightSum="30">

                    <TextView
                        android:id="@ id/kindler"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text=""
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="/"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="10"
                        android:gravity="center"
                        android:text="5"
                        android:textSize="17sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <!--Book Summary-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="Book Summary"
                android:textSize="@dimen/subTitle"
                android:textStyle="bold" />

            <TextView
                android:id="@ id/bookSummary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="add book summary"
                android:textSize="15sp"
                android:textStyle="italic" />

        </LinearLayout>

        <!-- Book Quotes -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="20"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Book Quotes"
                android:textSize="@dimen/subTitle"
                android:textStyle="bold" />

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="100dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@ id/quotebox1"
                        android:layout_width="250dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="10dp"
                        android:background="@drawable/quotebox"
                        android:text="add quote" />

                    <TextView
                        android:id="@ id/quotebox2"
                        android:layout_width="250dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="10dp"
                        android:background="@drawable/quotebox"
                        android:text="add quote" />

                    <TextView
                        android:id="@ id/quotebox3"
                        android:layout_width="250dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="10dp"
                        android:background="@drawable/quotebox"
                        android:text="add quote" />

                    <TextView
                        android:id="@ id/quotebox4"
                        android:layout_width="250dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="10dp"
                        android:background="@drawable/quotebox"
                        android:text="add quote" />


                    <TextView
                        android:id="@ id/quotebox5"
                        android:layout_width="250dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:layout_margin="10dp"
                        android:background="@drawable/quotebox"
                        android:text="add quote" />

                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>

        <!-- Reviews -->

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="REVIEWS"
                android:textSize="25sp"
                android:textStyle="bold" />


            <!--review 1-->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@ id/profilepic1"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_gravity="bottom"
                    android:background="@drawable/profilepic" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/reviewbubble"
                    android:orientation="vertical"
                    android:weightSum="100">

                    <TextView
                        android:id="@ id/review1"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_marginLeft="52dp"
                        android:layout_marginTop="35dp"
                        android:layout_marginRight="52dp"
                        android:layout_marginBottom="42dp"
                        android:layout_weight="80"
                        android:text="add a review"
                        android:textColor="@color/black"
                        android:textSize="15sp"
                        android:textStyle="italic" />

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="vertical"
            android:weightSum="100">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="20"
                android:text="rate this book"
                android:textSize="25sp" />

            <RatingBar
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="80" />

        </LinearLayout>

    </ScrollView>
</LinearLayout>

Thank you so much for trying to help, I appreciate it.

CodePudding user response:

Your layout structure is looks like this :

<LinearLayout>

    <ScrollView>

        <!-- Book Info -->
        <LinearLayout>

            <ImageView/>
            <LinearLayout>
                <TextView/>
                <TextView/>
                <TextView/>
                <TextView/>
                <TextView/>
            </LinearLayout>
            
        </LinearLayout>

        <!-- Ratings -->
        <LinearLayout>
            
            <!-- Good Reads -->
            <LinearLayout>

                <ImageView/>
                
                <LinearLayout>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                </LinearLayout>

            </LinearLayout>

            <!-- Common Sense Media -->
            <LinearLayout>

                <ImageView/>
                
                <LinearLayout>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                </LinearLayout>

            </LinearLayout>

            <!-- Read Print -->
            <LinearLayout>

                <ImageView/>
                
                <LinearLayout>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                </LinearLayout>

            </LinearLayout>

            <!-- Kindle -->
            <LinearLayout>

                <ImageView/>
                
                <LinearLayout>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <!--Book Summary-->
        <LinearLayout>

            <TextView/>
            <TextView/>

        </LinearLayout>

        <!-- Book Quotes -->
        <LinearLayout>

            <TextView/>

            <HorizontalScrollView>

                <LinearLayout>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                    <TextView/>
                </LinearLayout>
                
            </HorizontalScrollView>
            
        </LinearLayout>

        <!-- Reviews -->
        <LinearLayout>

            <TextView/>

            <!--review 1-->
            <LinearLayout>

                <de.hdodenhof.circleimageview.CircleImageView/>

                <LinearLayout>
                    <TextView/>
                </LinearLayout>
                
            </LinearLayout>
            
        </LinearLayout>

        <LinearLayout>

            <TextView/>
            <RatingBar/>

        </LinearLayout>

    </ScrollView>
    
</LinearLayout>

I tried to compile your layout codes and I found this error

ScrollView can host only one direct child

You can see on your layout structure, there are more than 1 child layouts under the scroll view.

The solution is make only one direct child under the scroll view. Maybe you can put 1 LinearLayout under the scroll view and after that, the rest of your code can be put inside 1 Linear Layout. Maybe like this :

<LinearLayout>

    <ScrollView>
    
        <LinearLayout>

            <!-- Book Info -->
            <!-- Ratings -->
                <!-- Good Reads -->
                <!-- Common Sense Media -->
                <!-- Read Print -->
                <!-- Kindle -->
            <!--Book Summary-->
            <!-- Book Quotes -->
            <!-- Reviews -->
                <!--review 1-->

        </LinearLayout>

    </ScrollView>
    
</LinearLayout>
  • Related