fix(SystemBars): keep the system bar backgrounds and navigation bar scrim

AD
5d ago
+16-3

Description

Since #8535, SystemBars sets its window insets listener on the DecorView. A listener is called instead of the view's own onApplyWindowInsets, and DecorView#onApplyWindowInsets is where the framework lays out the system bar backgrounds, including the contrast scrim behind the 3-button navigation bar. The scrim is therefore never drawn:

  • Android 15+, default template: no scrim.
  • Android 14 and older with EdgeToEdge.enable(this) (as the SystemBars docs recommend): no scrim.
  • Android 14 and older without edge-to-edge: the navigation bar loses its opaque background as well.

This PR makes three changes in initWindowInsetsListener():

  1. The listener calls the DecorView's own onApplyWindowInsets first, as the View.OnApplyWindowInsetsListener docs describe, and continues with the insets it returns. On windows that are not edge-to-edge, the DecorView reserves the navigation bar area for its background and consumes that inset; using its result keeps that area from being padded twice. On edge-to-edge windows it consumes nothing, so the insets are the same as before.
  2. The padding goes on the WebView's parent instead of the DecorView, as it did before #8535. The DecorView is the FrameLayout that holds the system bar background views, so padding it would move them into the content, for example a scrim band above the keyboard or above the navigation bar.
  3. If the WebView is not attached to a parent (for example a Fragment-hosted bridge whose view was destroyed), the listener returns the insets unchanged instead of padding a null view.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

Fixes #8724

Tests or Reproductions

Repro: https://github.com/adrenaline15/capacitor-systembars-scrim-repro. It is the default template with a page of alternating dark and light rows that prints env(safe-area-inset-*), --safe-area-inset-* and innerHeight. Buttons switch between viewport-fit=cover (insets passed through) and no cover (SystemBars pads the native view), and an input opens the keyboard. apply-fix.sh applies this PR to @capacitor/android 8.6.2.

Emulators with 3-button navigation: Android 16 (API 36, WebView 156) and Android 14 (API 34, WebView 157, also in landscape). Each was tested with and without EdgeToEdge.enable, with cover, without cover, and with the keyboard open.

Setup8.5.18.6.28.6.2 + this PR
API 36, default templatescrimno scrimscrim
API 36, EdgeToEdge.enablescrimno scrimscrim
API 34, EdgeToEdge.enablescrimno scrimscrim
API 34, default templateblack navigation barlight grey barblack navigation bar
  • API 36: every case is pixel-identical to 8.5.1 below the status bar, apart from the blinking text cursor.
  • API 34: the navigation bar matches 8.5.1 in every case, and the WebView keeps the size and position it has on 8.6.2, in portrait and landscape.
  • env(safe-area-inset-*), --safe-area-inset-* and innerHeight are the same as on 8.6.2.

npm run verify in android/ passes, and prettier reports no changes.

Screenshots / Media

"1st commit" is the first commit of this PR on its own, before the padding moved to the WebView's parent.
api34-edgetoedge-keyboard
api34-edgetoedge-navbar
api34-edgetoedge-nocover-navbar
api34-landscape-nocover-navbar
api34-navbar
api36-edgetoedge-keyboard
api36-edgetoedge-navbar
api36-navbar
api36-nocover-navbar

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

  • The same code is on 8.x, and both commits cherry-pick onto it cleanly.
  • Not tested below API 34.
  • On API 34 without edge-to-edge, --safe-area-inset-top is still 49px although the window starts below the status bar (#8623). This PR does not change that.