CoordinatorLayout status bar padding disappears from ViewPager 2nd page
A

4

23

EDIT of 01/02/2016: Bug should be resolved by applying the code provided by Android Team: https://mcmap.net/q/565958/-coordinatorlayout-status-bar-padding-disappears-from-viewpager-2nd-page, see accepted answer below.

EDIT of 27/01/2016: Bug still not resolved in v23.1.1. Solutions provided until now don't give transparent status bar (which is the purpose of this layout) or are too complex. A new screen-record of the bug available here: https://www.youtube.com/watch?v=76IxhlUx8MQ

EDIT of 23/07/2015: Support Design Library v22.2.1 didn't fix :-( Also this happens on toolbar quick return on MainActivity!

EDIT of 28/07/2015: Linked question: CoordinatorLayout status bar padding disappears during fragment transactions

From example repository https://github.com/chrisbanes/cheesesquare I've implemented the ViewPager on Detail Activity. It works, but the StatusBar disappears from 2nd page as you see in the picture only in Lollipop devices, Any idea? I use android:fitsSystemWindows="true" but it does work only on first page :-(

enter image description here

activity_detail_viewpager.xml if I put here fitsSystemWindows, StatusBar is not transparent anymore, but it works (status bar padding is not lost). But I would like it transparent!

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="ifContentScrolls"/>

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

activity_detail_fragment.xml

<CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        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="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/back_item"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <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"
                app:layout_collapseMode="pin" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/card_margin">

                ...

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

            ...

        </LinearLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:src="@drawable/ic_discuss"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|end"
        app:borderWidth="0dp"/>

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

style.xml v21

<resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>
Ameliorate answered 12/7, 2015 at 14:22 Comment(1)
Posted issue to Google: code.google.com/p/android/issues/detail?id=180492 - Issue is confirmed also by others pplAmeliorate
A
17

Solution proposed by Android Team in the answer of my reported defect. This code should finally work.

ViewPager mViewPager;

ViewCompat.setOnApplyWindowInsetsListener(mViewPager,
        new OnApplyWindowInsetsListener() {
    @Override
    public WindowInsetsCompat onApplyWindowInsets(View v,
            WindowInsetsCompat insets) {
        insets = ViewCompat.onApplyWindowInsets(v, insets);
        if (insets.isConsumed()) {
            return insets;
        }

        boolean consumed = false;
        for (int i = 0, count = mViewPager.getChildCount(); i <  count; i++) {
            ViewCompat.dispatchApplyWindowInsets(mViewPager.getChildAt(i), insets);
            if (insets.isConsumed()) {
                consumed = true;
            }
        }
        return consumed ? insets.consumeSystemWindowInsets() : insets;
    }
});
Ameliorate answered 1/2, 2016 at 13:47 Comment(1)
Three years later in this issue still happens. Do you know any thread that Google was discussing this issue? Btw, great solution!Crosslet
I
3

Like sidecarcat, I ran into a similar issue (using v23.1.1). I post here a workaround by using the code of sidecarcat and add some code to remove superflous padding in some cases.

    // in onCreateView: adjust toolbar padding
    final int initialToolbarHeight = mToolbar.getLayoutParams().height;
    final int initialStatusBarHeight = getStatusBarHeight();
    mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int[] locToolbar = new int[2];
            mToolbar.getLocationOnScreen(locToolbar);
            int yToolbar = locToolbar[1];
            int topPaddingToolbar = mToolbar.getPaddingTop();
            if (isAdded()) {
                //normal case : system status bar padding on toolbar : yToolbar = initialStatusBarHeight && topPaddingToolbar = 0
                //abnormal case : no system status bar padding on toolbar -> toolbar behind status bar => add custom padding
                if (yToolbar != initialStatusBarHeight && topPaddingToolbar == 0) {
                    mToolbar.setPadding(0, initialStatusBarHeight, 0, 0);
                    mToolbar.getLayoutParams().height = initialToolbarHeight + initialStatusBarHeight;
                }
                //abnormal case : system status bar padding and custom padding on toolbar -> toolbar with padding too large => remove custom padding
                else if (yToolbar == initialStatusBarHeight && topPaddingToolbar == initialStatusBarHeight) {
                    mToolbar.setPadding(0, 0, 0, 0);
                    mToolbar.getLayoutParams().height = initialToolbarHeight;
                }
            }
        }
    });

    return mRootView;
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}
Interdenominational answered 6/1, 2016 at 17:16 Comment(0)
S
0

I ran into a similar issue (using v23.0.1). In my case, the problem occurs on the first page as well. The workaround I settled on was to adjust the padding and height of the toolbar when the fragment is created.

    // in onCreateView: adjust toolbar padding
    Toolbar toolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + getStatusBarHeight();

    return mRootView;
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}
Saberio answered 6/10, 2015 at 18:12 Comment(1)
Nice, it's a beginning, and something starts to work (as a fix), but scrolling left and right it happens on some pages to have that padding now superfluous, is there a way to know if the status bar padding is became again visible so to avoid to add the status bar height? We should retrieve the real height of the status bar. I guess.Ameliorate
A
-2

Seems the solution for this is simple

You have

android:fitsSystemWindows="true"

In the fragment CoordinateLayoutView

remove it from here and put in the Activity's root view.

This should now all work

Affined answered 6/11, 2015 at 22:4 Comment(2)
Hello, the purpose is to have it transparent, so this solution cannot be accepted. I think, design library works for normal and easy cases while it has big issues (and not only this described in my question) in complex cases. I almost there to abandon this library in case of viewpager.Ameliorate
please show me the code, I have transparent/status bar coloured if that is the picture on googlecode then it looks like you may have android:fitsSystemWindows="true" in a couple of placesAffined

© 2022 - 2024 — McMap. All rights reserved.