Search code examples
android-layoutandroid-scrollviewandroid-relativelayout

ScrollView overlapping other content in RelativeLayout


I have a drawer menu defined for my app that uses a RelativeLayout as its root.

The general structure of the menu is as follows:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:importantForAccessibility="no"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/linear_vert_drwr_hdr"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:padding="8dp"
    android:background="@drawable/drawer_bg"
    android:orientation="vertical">

    <ImageView
        ... />

    <TextView
        ... />

</LinearLayout>

<LinearLayout
    android:id="@+id/manage_ids"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linear_vert_drwr_hdr"
    android:padding="8dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <TextView
        ... />

    <ImageView
        ... />

</LinearLayout>

<LinearLayout
    android:id="@+id/prefs_section"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/manage_ids"
    android:padding="8dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <TextView
        ... />
</LinearLayout>

<ScrollView
    android:id="@+id/vscroll_prefs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/prefs_section"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp">
    <LinearLayout
        android:id="@+id/linear3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:orientation="vertical"
        android:layout_gravity="start">

        <Switch
            android:id="@+id/switch_rem_last_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        ... />

        ... a bunch of switches ...
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/i_div_line_top"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:contentDescription="@string/dividing_line"
        android:paddingEnd="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingStart="10dp"
        app:srcCompat="@drawable/line_box" />


    <TextView
        ... />

    <TextView
        ... />
    <ImageView
        ... />

    <LinearLayout
        android:id="@+id/social_links"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            ... />

        <ImageView
            ... />

    </LinearLayout>

    <TextView
        android:id="@+id/l_app_version"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="8dp"
        android:text="@string/app_version"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="12sp"
        android:textStyle="italic" />

</LinearLayout>

</RelativeLayout>

This works fine on phones with large screens, but on smaller screens the ScrollView overlaps the LinearLayout below it.

As I'm using a RelativeLayout as the root of the layout I can't use android:layout_height="0dp" and android:layout_weight="1" instead of android:layout_height="wrap_content".

Is there another way to make the ScrollView expand to fill the available space but no more?


Solution

  • Got it.

    I gave the LinearLayout below the ScrollView and id, then in the ScrollView I defined android:layout_above="@id/layout_below_the_scrollview".

    Simple when you realise just how many android:layout... options there are!