Search code examples
androidandroid-layoutscrollviewandroid-scrollview

Contents of ScrollView leaking out of the parent view in Android


The layout file below should give you the idea that I want the contents of scroll view inside the RelativeLayout below the TextView, but the contents of the ScrollView go out of the RelativeLayout to get hidden behind the TextView. I don't understand how this is happening. In the RelativeLayout inside the ScrollView I'm adding a LinearLayoutCompat object programmatically and to that object I am adding several AppCompatTextView object also programmatically. The screenshot is just 1 instance of the layout.

I am setting the nested RelativeLayout to start below the TextView but still it starts from the very top.

layout.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/narrowTextnon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/cherryRed"
        android:elevation="8dp"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:padding="25dp"
        android:text="Let's narrow down now !"
        android:textColor="@color/wineBrown"
        android:textSize="22sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/narrowTextnon"
        android:gravity="center">

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

            <RelativeLayout
                android:id="@+id/dataContnon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

            </RelativeLayout>

        </ScrollView>

    </RelativeLayout>

</RelativeLayout>

enter image description here


Solution

  • Finally got what I wanted by applying :
    android:layout_height="match_parent" to nested RelativeLayout to position the ScrollView in center
    android:layout_below="@id/narrowText" to have the ScrollView start from below the TextView instead of the very top.
    android:layout_alignParentBottom = true to have the ScrollView stretch from the bottom of the TextView to the bottom of the screen.