Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout is getting cut off inside ScrollView Android

I have an activity in my app where I would like the user to be able to vertically scroll the content contained inside a LinearLayout which in turn is inside a ScrollView. Here is a summary of what the layout XML for this activity looks like:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dip"
            android:orientation="vertical">

            <!-- a bunch of standard widgets, omitted for brevity -->

            <!-- everything renders but starting in this TextView -->
            <!-- content begins to get truncated -->
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:gravity="left"
                android:textSize="20dip"/>

            <!-- this View, really just a trick for a horizontal row, is -->
            <!-- completely cutoff -->
            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:background="@color/green" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

What I am observing is that content in the inner LinearLayout is being cutoff inside the ScrollView. In the final TextView above, when it contains little text, then the View below it does render in portrait, but does not in landscape. When this TextView contains a lot of text, then it gets cutoff in portrait mode.

I tried the recommendations I found on Stack Overflow. Adding bottom padding to the ScrollView did not resolve the problem, nor did swapping the ScrollView for a NestedScrollView.

Any helpful suggestions would be welcome. This is actually turning out to be a blocker.

like image 684
Tim Biegeleisen Avatar asked Jul 25 '17 14:07

Tim Biegeleisen


1 Answers

Change your inner LinearLayout's margin to padding. Or, if you really need it to be margin (maybe you're using a custom background), wrap your LinearLayout in a FrameLayout.

The ScrollView is taking its height (or, more accurately, it is computing its scrollable range) from the LinearLayout. This value doesn't include margins, so your ScrollView is going to be "too short" by the sum of the LinearLayout's top and bottom margins.

like image 153
Ben P. Avatar answered Nov 04 '22 16:11

Ben P.