ScrollView in Android does not work

I am new to Android app development and I have the following problem. When I add a ScrollView to my layout I got the following message:

Rendering Problems NOTE: This project contains Java compilation errors, which can cause rendering failures for custom views. Fix compilation problems first. Exception raised during rendering: ScrollView can host only one direct child

I don't know exactly what I have to do to fix this.

My layout:

<?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="@drawable/home"
tools:context="dmst.allamoda.Login">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:weightSum="1">


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/linearLayout1"
    android:background="@drawable/rectangle"
    android:gravity="top|right">

    <Button
        android:background="@drawable/mybutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/button_home"
        android:id="@+id/home"
        android:textColor="#ffffd1"
        android:onClick="signupControl"
        android:layout_gravity="center"/>

    <Button
        android:background="@drawable/mybutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/button_profile"
        android:id="@+id/profile"
        android:textColor="#ffffd1"
        android:onClick="signupControl"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/logo_image"
        android:background="@drawable/allamodalogo"
        android:layout_width="160dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:gravity="right"
        android:adjustViewBounds="true"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_gravity="right"/>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/linearLayout2"
    android:layout_marginTop="20dp"
    android:background="@drawable/post"
    android:gravity="top|left">

    <ImageView
        android:id="@+id/profile2"
        android:background="@drawable/profile2"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:gravity="right"
        android:adjustViewBounds="true"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_gravity="right"/>

    <TextView
        android:id="@+id/username"
        android:inputType="text"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:textColorHint="#000000"
        android:hint="@string/myprofile"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:textColor="#000000"
        android:gravity="top"/>
</LinearLayout>


<LinearLayout
    android:layout_marginTop="25dp"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    android:id="@+id/linearLayout3"
    android:background="@drawable/post">

    <LinearLayout
        android:layout_marginTop="0dp"
        android:layout_marginBottom="10dp"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:orientation="horizontal"
        android:layout_gravity="left"   >

        <ImageView
            android:id="@+id/profile_image"
            android:background="@drawable/profile2"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:scaleType="fitXY"
            android:gravity="right"
            android:adjustViewBounds="true"
            android:maxHeight="50dp"
            android:maxWidth="50dp"
            android:minHeight="50dp"
            android:minWidth="50dp"/>

        <TextView
            android:id="@+id/postname"
            android:inputType="text"
            android:layout_width="190dp"
            android:layout_height="50dp"
            android:textColorHint="#000000"
            android:hint="@string/myprofile"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="15dp"
            android:textColor="#000000"
            android:gravity="top"/>

    </LinearLayout>

    <ImageView
        android:id="@+id/post1"
        android:background="@drawable/post2"
        android:layout_width="150dp"
        android:layout_height="160dp"
        android:scaleType="fitXY"
        android:gravity="center"
        android:adjustViewBounds="true"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:marginBottom="10dp"/>


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

Upvotes: 0

Views: 127

Answers (2)

Junior Mourao
Junior Mourao

Reputation: 340

Just make the top-level layout a ScrollView:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <LinearLayout
            <!-- everything you w -->
    </LinearLayout>
</ScrollView>

work for me:How to use ScrollView in Android?

Upvotes: 1

mixel
mixel

Reputation: 25836

ScrollView must contain only one child. You should wrap all LinearLayout views in one vertical LinearLayout and put it inside ScrollView:

<ScrollView
    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:background="@drawable/home"
    tools:context="dmst.allamoda.Login">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    ...
    </LinearLayout>
</ScrollView>

Also I removed root LinearLayout because it seems redundant.

Upvotes: 3

Related Questions