Search code examples
javaandroidxmlscrollviewnestedscrollview

Nested ScrollView leaves some extra space on top


I have a NestedScrollView under AppBarLayout with only one child - LinearLayout. Both of them have no padding and no margin on top, but still there is some space left (belonging to NestedScrollView, as I can see, setting color for the background) on top of the NestedScrollView. How to remove it?

I have tried setting android:fillViewport="true" in NestedScrollView, but got no result.

UPD: this space interacts with clipNoPadding flag, but setting all paddings to 0 does not help.

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:background="@color/colorAccent"
        android:fillViewport="true"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:orientation="vertical">

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

            // and some more buttons, just to fill the space

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        android:background="@color/colorTransparent">

        // I omit some code, as not-belonging to the question

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

how it looks


Solution

  • The android:fitsSystemWindows="true" attribute will add some (usually, but not always, 24dp) top padding to the view it is applied to. This is meant to be used alongside a transparent or translucent status bar (or navigation bar), so that your view's content doesn't appear "under" the status bar.

    In your screenshot, it doesn't look like you're using a transparent status bar, so this will just add 24dp of padding that you don't want. Remove it.

    Note also that android:fitsSystemWindows will override any other padding on the view that uses it. So even if you set the padding to 0 manually, you'll still get 24dp padding.