Search code examples
androidandroid-layoutandroid-linearlayoutandroid-scrollview

ScrollView does not scroll when children increase in height


I have a ScrollView that holds 2 fragments. The bottom fragment has 2 EditText views. When I enter a multi-line text in either of the EditTexts, the overall height requirement for the views on the screen naturally increases. However, the screen cuts off anything that is pushed due to multi-line text input. ScrollView does not scroll at that point.

I've tried some suggested methods on SO like not making ScrollView the root view or adding an empty view as the last grandchild of the scrollview but none of them worked so far. This is my XML:

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

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

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:context=".MainActivity">

            <fragment
                android:id="@+id/fragment1"
                android:name="com.example.omar.memegenerator.TopImageFragment"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="18dp"
                android:layout_marginBottom="18dp"
                tools:layout="@layout/fragment_top_image" />

            <fragment
                android:id="@+id/fragment2"
                android:name="com.example.omar.memegenerator.BottomControlsFragment"
                android:layout_width="match_parent"
                android:layout_height="134dp"
                android:layout_below="@+id/fragment1"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="34dp"
                tools:layout="@layout/fragment_bottom_controls" />

        </RelativeLayout>
    </ScrollView>

</LinearLayout>

Solution

  • Use this for your layout hierarchy instead. I've omitted some attributes, but the ones included are key to getting this to work.

    <ScrollView
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="vertical">
    
            <fragment
                android:id="@+id/fragment1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
            <fragment
                android:id="@+id/fragment2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
       </LinearLayout>
    
    </ScrollView>