Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoordinatorLayout status bar padding disappears during fragment transactions

With the 22.2.1 Design Support Library and API 22(have not tested on earlier versions yet), I'm running into issues with the status bar padding when switching between fragments. The initial fragment loads fine, but after a fragment transaction, the status bar padding disappears, pushing all the views up where they shouldn't be. The same thing happens to the original fragment after popping the back stack. Rotating the device fixes it, as does opening the soft keyboard(but only in portrait, not in landscape).

main fragment on initial load or after rotation(desired)

main fragment after back pressed

other fragment when loaded

other fragment after rotation or keyboard(desired)

main fragment:

<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"
tools:context=".MainFragment">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    style="@style/RecyclerView"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Fab"
    android:src="@drawable/ic_person_add_white_24dp"
    app:backgroundTint="@color/accent_dark"
    app:borderWidth="2dp"/>

second fragment:

<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.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/card_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/collapsingToolbar_height"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="@dimen/default_margin"
        app:expandedTitleMarginEnd="@dimen/sheet_expanded_title_margin"
        app:expandedTitleMarginStart="@dimen/sheet_expanded_title_margin"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/default_tab_layout_height"
        app:tabGravity="center"
        app:tabMinWidth="120dp"
        app:tabMode="scrollable"/>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Fab"
    android:src="@drawable/ic_add_white_24dp"
    app:backgroundTint="@color/accent_dark"
    app:borderWidth="2dp" />

Themes have windowDrawsSystemBarBackgrounds to true and statusBarColor to transparent.

like image 689
Jim Pekarek Avatar asked Jul 23 '15 02:07

Jim Pekarek


1 Answers

Solved it, thanks to Chris Banes.

The problem is that it doesn't know the window insets. You have to requestApplyInsets in onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ViewCompat.requestApplyInsets(coordinatorLayout);
}
like image 63
Jim Pekarek Avatar answered Nov 05 '22 17:11

Jim Pekarek