Make window inset application idempotent

The inset listener applied the status bar padding only once,
guarded by a paddingTop == 0 check, and grew the toolbar height
incrementally. When the first callback reported wrong insets the
layout stayed stuck with stale values, so affected devices only
settled after several rotations.

Recompute the padded toolbar height from a captured bare height
on every callback and always set the nav bar padding, including
back to zero, so the layout converges on the first correct inset
pass.
This commit is contained in:
Markus Fisch
2026-06-10 21:19:44 +02:00
parent 7546cf25c0
commit dcecec4d6f
@@ -20,23 +20,25 @@ fun setPaddingFromWindowInsets(
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
return
}
// Capture the bare toolbar height so the listener can recompute the
// padded height from scratch every time. Applying the insets only
// once (or adding them incrementally) leaves the layout stuck with
// stale values when the first callback reports wrong insets, which
// is why some devices only settled after several rotations.
val baseToolbarHeight = toolbarHeight
mainLayout.setOnApplyWindowInsetsListener { _, insets ->
val systemBarInsets = insets.getInsets(
WindowInsets.Type.systemBars()
)
val statusBarTop = systemBarInsets.top
if (toolbar.paddingTop == 0 && statusBarTop > 0) {
toolbar.setPadding(0, statusBarTop, 0, 0)
toolbar.layoutParams.height += statusBarTop
}
toolbar.setPadding(0, statusBarTop, 0, 0)
toolbar.layoutParams.height = baseToolbarHeight + statusBarTop
val navBarBottom = systemBarInsets.bottom
if (navBarBottom > 0) {
mainLayout.setPadding(0, 0, 0, navBarBottom);
navbar?.apply {
layoutParams.height = navBarBottom
}
mainLayout.setPadding(0, 0, 0, navBarBottom)
navbar?.apply {
layoutParams.height = navBarBottom
}
insets