ikon ikon - 5 months ago 8
Android Question

ScrollView is only showing single view inside its sub-hierarchy

I am supposed to add a

ScrollView
, to enable user to scroll down after the button. However, it is not showing what it supposed to show.

Whatever I add after the
Button
, doesn't show up on device/emulator. I am supposed to add
ImageView
under the
Button
, but it seem to not displaying anything after the
Button
.

Following is my XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Description:"
android:id="@+id/textView14"
android:paddingTop="20dp"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Restaurant Location:"
android:id="@+id/textView15"
android:paddingTop="20dp"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_below="@+id/imageView3"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ScrollView>

Answer

The problem is that you are not aligning the views correctly. For example, there's no editText2, editText and imageView3 and you're aligning your views around left or below of them.

Either replace RelativeLayout with LinearLayout or use this XML (I've replaced those unknown IDs with correct ones):

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">   
        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Description:"
            android:paddingTop="20dp" />
        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Restaurant Location:"
            android:paddingTop="20dp"
            android:layout_below="@+id/textView14" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:layout_below="@id/textView15"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</ScrollView>