android:fitsSystemWindows not working

Taig picture Taig · Jan 29, 2015 · Viewed 26.9k times · Source

In my theme I defined the following rules to draw my views behind the status bar:

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>

And in the Activity (onCreate):

getWindow.getDecorView.setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)

Then, in my View im using a Barcodescanner that is supposed to draw behind the status bar, which is working. However when I apply the android:fitsSystemWindows to any child view they don't get their position adjusted. It's working when I apply it to the root element, though.

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

    <my.Scanner
        android:id="@+id/scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true"> <!-- Should receive a top padding, right? -->

        <View android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/red" />

    </FrameLayout>

</FrameLayout>

Screenshot

Answer

Ali Karaca picture Ali Karaca · Sep 14, 2015

Use CoordinatorLayout as root for your view, it worked for me.

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">