SetFitsSystemWindow programmatically does not work properly

I am trying to work with various AppBarLayouts in one action.
When the Fragment is loaded, it can call the "ToolbarManager", which handles the replacement of the active toolbar (namely, AppBarLayout). For some of these toolbars (using CollapsingToolbarLayout) I want them to draw for the StatusBar.
This works great when setting the fitsSystemWindow parameter to true in XML. But when using only the toolbar with the layout_scrollFlags="scroll"title of the toolbar is displayed even behind the StatusBar.

To avoid this, I want to disable "fitsSystemWindow" when loading such a toolbar with setFitsSystemWindow(false).

Disabling works fine, but when you re-enable using setFitsSystemWindow(true)to use with CollapsingToolbarLayout, the StatusBar is colored, and the toolbar is placed under the StatusBar with the addition of the StatusBar size on top.

Image: shown below StatusBar instead

When I don't use setFitsSystemWindow(true)and just set it to true in the XML layout, the CollapsingToolbarLayout display is fine.

Is this a bug in setFitSystemWindowor this method is different from an XML attribute?

Here are the layouts that I use:

Activity:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:context=".activities.MainActivity"
    style="@style/AppTheme">

<android.support.design.widget.CoordinatorLayout
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinator_layout">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="wrap_content">

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


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </FrameLayout>

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:indeterminateDuration="1000"
        android:layout_marginBottom="-7dp"
        android:id="@+id/loadingBar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_gravity="center_horizontal|bottom"
        android:gravity="bottom"/>
</android.support.design.widget.CoordinatorLayout>

<include layout="@layout/include_navigationview" />

AppBarLayout is replaced every time a new fragment is loaded.

The next layout is a toolbar that should not draw behind the StatusBar, but the toolbar should scroll (leaving only the TabLayout visible)

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:id="@+id/toolbar_discover"
    android:fitsSystemWindows="true"
    android:paddingBottom="0dp">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:theme="@style/AppTheme.Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|snap">

    </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout_discover"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:tabMode="fixed"
            app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>

, fitsSystemWindows=false CoordinatorLayout MainActivity. CollapsingToolbar StatusBar.

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:id="@+id/toolbar_account"
    android:fitsSystemWindows="true"
    android:paddingBottom="0dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:id="@+id/collapsingToolbarLayout">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"
            android:id="@+id/banner_image"
            android:src="@mipmap/img_header"/>

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:id="@+id/toolbar"
            android:fitsSystemWindows="false"
            android:layout_height="?attr/actionBarSize"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:theme="@style/AppTheme.Toolbar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>

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

All Articles