xref: /aosp_15_r20/external/accompanist/docs/systemuicontroller.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1# System UI Controller for Jetpack Compose
2
3[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-systemuicontroller)](https://search.maven.org/search?q=g:com.google.accompanist)
4
5!!! warning
6    **This library is deprecated, and the API is no longer maintained. We recommend forking the implementation and customising it to your needs.** The original documentation is below.
7
8## Migration
9Recommendation: If you were using SystemUIController to go edge-to-edge in your activity and change the system bar colors and system bar icon colors, use the new [Activity.enableEdgeToEdge](https://developer.android.com/reference/androidx/activity/ComponentActivity#(androidx.activity.ComponentActivity).enableEdgeToEdge(androidx.activity.SystemBarStyle,androidx.activity.SystemBarStyle)) method available in androidx.activity 1.8.0-alpha03 and later. This method backports the scrims used on some versions of Android. [This](https://github.com/android/nowinandroid/pull/817) is a sample PR of the migration to the new method and removing the dependency on SystemUIController in Now in Android.
10
11For other usages, migrate to using WindowInsetsControllerCompat or window APIs directly.
12
13## Original Documentation
14System UI Controller provides easy-to-use utilities for updating the System UI bar colors within Jetpack Compose.
15
16## Usage
17To control the system UI in your composables, you need to get a [`SystemUiController`](../api/systemuicontroller/systemuicontroller/com.google.accompanist.systemuicontroller/-system-ui-controller/) instance. The library provides the [`rememberSystemUiController()`](../api/systemuicontroller/systemuicontroller/com.google.accompanist.systemuicontroller/remember-system-ui-controller.html) function which returns an instance for the current system (currently only Android).
18
19In your layouts you can update the system bar colors like so:
20
21``` kotlin
22// Remember a SystemUiController
23val systemUiController = rememberSystemUiController()
24val useDarkIcons = !isSystemInDarkTheme()
25
26DisposableEffect(systemUiController, useDarkIcons) {
27    // Update all of the system bar colors to be transparent, and use
28    // dark icons if we're in light theme
29    systemUiController.setSystemBarsColor(
30        color = Color.Transparent,
31        darkIcons = useDarkIcons
32    )
33
34    // setStatusBarColor() and setNavigationBarColor() also exist
35
36    onDispose {}
37}
38```
39
40## System bar icon colors
41The library automatically handles API level differences when running on Android devices. If we look at the example
42of status bar icons, Android only natively supports dark icons on API 23+. This library handles this by automatically
43altering the requested color with a scrim, to maintain contrast:
44
45![](api-scrim.png)
46
47Similar happens on navigation bar color, which is only available on API 26+.
48
49### Modifying scrim logic
50
51The scrim logic can be modified if needed:
52
53``` kotlin
54systemUiController.setStatusBarColor(
55    color = Color.Transparent,
56    darkIcons = true
57) { requestedColor ->
58    // TODO: return a darkened color to be used when the system doesn't
59    // natively support dark icons
60}
61```
62
63## Samples
64
65For complete samples, check out the [Insets samples](https://github.com/google/accompanist/tree/main/sample/src/main/java/com/google/accompanist/sample/insets) which all use `SystemUiController` to set transparent system bars.
66
67## Download
68[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-systemuicontroller)](https://search.maven.org/search?q=g:com.google.accompanist)
69
70```groovy
71repositories {
72    mavenCentral()
73}
74
75dependencies {
76    implementation "com.google.accompanist:accompanist-systemuicontroller:<version>"
77}
78```
79
80Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. These are updated on every commit.
81
82[compose]: https://developer.android.com/jetpack/compose
83[snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/accompanist/accompanist-systemuicontroller/
84