Samiur Rahman Samiur Rahman - 6 months ago 13
Android Question

implement scrollView in form

I am trying to add

ScrollView
in my form. But whenever I add
ScrollView
it is showing "
ScrollView
can host only one direct child"
.What does that mean?And How can I implement
ScrollView
in my form? Here is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

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

<ImageView
android:id="@+id/showImage"
android:layout_width="400dp"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
<!--android:src="@drawable/placeholder"-->
<Button
android:id="@+id/btnSaveImage"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center_horizontal"
android:background="#f66565"
android:text="Select Image" />

<EditText
android:id="@+id/editFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Blog Title">

<requestFocus />
</EditText>

<EditText
android:id="@+id/editLastName"
android:layout_width="match_parent"
android:layout_height="150dp"
android:ems="10"
android:hint="Enter Blog Description" />

<EditText
android:id="@+id/editWeb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Web Link">

<requestFocus />
</EditText>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">

<Button
android:id="@+id/btnNewUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#f66565"
android:text="New Blog" />

<Button
android:id="@+id/btnSaveRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#f66565"
android:text="Save Blog" />

<Button
android:id="@+id/btnfullinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#f66565"
android:text="Show All" />

</LinearLayout>
</ScrollView>

</LinearLayout>

Answer

scroll view can contain only one child view. so you should use the top view as scroll view and make a child view as linear or relative or other as you want. then add all other views like button or text view in that linear or relative layout. you can't add more child view in a scroll view. here your code should be like:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <ImageView
            android:id="@+id/showImage"
            android:layout_width="400dp"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />
        <!--android:src="@drawable/placeholder"-->
        <Button
            android:id="@+id/btnSaveImage"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_gravity="center_horizontal"
            android:background="#f66565"
            android:text="Select Image" />

        <EditText
            android:id="@+id/editFirstName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Blog Title">

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editLastName"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:ems="10"
            android:hint="Enter Blog Description" />

       <EditText
            android:id="@+id/editWeb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ems="10"
            android:hint="Enter Web Link">

            <requestFocus />
       </EditText>

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="20dp">

            <Button
                android:id="@+id/btnNewUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="#f66565"
                android:text="New Blog" />

           <Button
                android:id="@+id/btnSaveRecord"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="#f66565"
                android:text="Save Blog" />

           <Button
                android:id="@+id/btnfullinfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="#f66565"
                android:text="Show All" />

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