How to automatically color Android's status bar based on appbar

48 Views Asked by At

According to the material 3 specs of the appbar not only the appbar itself, also the statusbar should adapt it's color when scrolling the list under the appbar. How to achieve this? Are we supposed to do this manually in each screen? https://m3.material.io/components/top-app-bar/guidelines

1

There are 1 best solutions below

1
On

In each screen where you want the status bar to adapt its color, you can manually handle this behavior. Use WindowInsetsController and getWindowInsetsController() method. This method is available from Android 11:

// Obtain an instance of the WindowInsetsController
val windowInsetsController = window?.insetsController

// Set the appearance of the system bars
windowInsetsController?.setSystemBarsAppearance(APPEARANCE_STABLE_TOP, APPEARANCE_STABLE_TOP)

// For example, you can listen to the scroll events and update the appearance flags accordingly