ViewGroup inside CollapsingToolbarLayout show extra bottom padding when set fitsSystemWindows to be true

this problem maybe caused by this commit

I fixed it programmatically with the way comment by @Bogdan Zurac.

mCollapseContentLy = findViewById(R.id.ly_collapse_content);
mCollapseContentLy.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int measuredHeight = mCollapseContentLy.getMeasuredHeight();
        ViewGroup.LayoutParams lp = mCollapsingToolbarLayout.getLayoutParams();
        layoutParams.height = measuredHeight;
        mCollapsingToolbarLayout.setLayoutParams(lp);
        mCollapseContentLy.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
});

Edit after accepted

create a new class extend CollapsingToolbarLayout and override the onMeasure method

package android.support.design.widget;

import android.content.Context;
import android.util.AttributeSet;

public class GodCollapsingToolbarLayout extends CollapsingToolbarLayout {

    public GodCollapsingToolbarLayout(Context context) {
        this(context, null);
    }

    public GodCollapsingToolbarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GodCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int mode = MeasureSpec.getMode(heightMeasureSpec);
        final int topInset = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;
        if (mode == MeasureSpec.UNSPECIFIED && topInset > 0) {
            // fix the bottom empty padding
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                    getMeasuredHeight() - topInset, MeasureSpec.EXACTLY);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

Based on @Akshay answer this is my solution:

In the xml file add android:fitsSystemWindows="true" to:

  1. CoordinatorLayout
  2. AppBarLayout
  3. ImageView (or other view you want to see behind the status bar. In my case it was a ConstraintLayout)

In the Activity add @Akshay answer but edit it as follow (written in Kotlin):

 ViewCompat.setOnApplyWindowInsetsListener(appBarLayout) { _, insets ->
        // Instead of
        // toolbar.setPadding(0, insets.systemWindowInsetTop, 0, 0)
        (toolbar.layoutParams as ViewGroup.MarginLayoutParams).topMargin = insets.systemWindowInsetTop
        insets.consumeSystemWindowInsets()
    }

Now the toolbar will have the proper height as well.


I recently faced a similar issue, this was due to the insets,as the collapsing toolbar is drawn behind the statusbar , it provides the additional height of the status bar, in order to avoid this issue, I found a better solution , instead of a calculating a height of collapse toolbar, let the toolbar consume the insets

ViewCompat.setOnApplyWindowInsetsListener(appBarLayout) { v, insets ->
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            toolBar.setPadding(0, insets.systemWindowInsetTop, 0, 0)
        }
        insets.consumeSystemWindowInsets()
    }

This thing worked , instead of calculating the height I would recommend to go through the following links

Ian Lakes article

Chris Banes talk

Give it a try