I found a solution for my case on APIs from 28 to 26. Because on higher versions everything was fine.
First, I need to mention, that I've tried everything mentioned in the answers above but nothing helped.
The solution:
ViewCompat.setOnApplyWindowInsetsListener(your_parent_view) { v, insets ->
var consumed = false
(v as ViewGroup).forEach { child ->
// Dispatch the insets to the child
val childResult = ViewCompat.dispatchApplyWindowInsets(child, insets)
// If the child consumed the insets, record it
if (childResult.isConsumed) {
consumed = true
}
}
// If any of the children consumed the insets, return an appropriate value
if (consumed) WindowInsetsCompat.CONSUMED else insets
}
In my case your_parent_view
was a FragmentViewContainer
where my Fragment was located with the AppBarLayout
to which I want to add padding.
It's not my finding, this is mentioned in Chris Banes's article, as a fix for another issue.
In addition, I've removed all the fitsSystemWindows=true
lines and left this attribute only in my AppBarLayout
, in the current fragment.
And of course, this line should be added to your Activity:
WindowCompat.setDecorFitsSystemWindows(window, false)
The additional ViewCompat.setOnApplyWindowInsetsListener(appBarLayout)
was not required. The fitsSystemWindows=true
attribute was doing its work.