0

I'm very new at coding, I'm in Android Basics at Udacity. I have a constraint layout with various TextViews and ImageViews and need to add a scroll bar for an assignment. I tried adding the scrollview outside of the layout, that made the app crash, then in the layout with the other views as children of the scrollview within the constraint layout, but an error came up stating that there can be only one child for the scroll view. Why is that? How do I add a scrollview?

`<?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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Hello World"
        android:textSize="25sp"
        android:textColor="#990099" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Words"
        android:textSize="25sp"
        android:textColor="#990099"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Words"
        android:textSize="25sp"
        android:textColor="#990099"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Words"
        android:textSize="25sp"
        android:textColor="#990099"/>
    </ScrollView>
</LinearLayout>
`

UPDATE I tried this, but the app crashes.

1 Answer 1

3

When using a ScrollView, it can only have one child. You'll usually want to use a LinearLayout, and that is where you'll put your actual content.

<ScrollView>
    <LinearLayout>
         <!-- Your Content --> 
    </LinearLayout>
</ScrollView>

This will give the ScrollView the knowledge it needs to scroll. Just make sure to set the orientation of your LinearLayout, such as vertical.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="Hello World"
            android:textColor="#990099"
            android:textSize="25sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="Words"
            android:textColor="#990099"
            android:textSize="25sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="Words"
            android:textColor="#990099"
            android:textSize="25sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="Words"
            android:textColor="#990099"
            android:textSize="25sp" />

    </LinearLayout>
</ScrollView>
Sign up to request clarification or add additional context in comments.

4 Comments

I always get an error when I add the scroll view outside the linear layout
Namespace Android is not found
I updated my answer, using your layout. This works fine for me.
No problems. Have fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.