4

I'm following the theming tutorial from developer.google. I'm trying to migrate the app to Mat. 3 and modify the status bar's color to match with the background color.

  • After adding android:statusBarColor and android:windowLightStatusBar, nothing changes.

  • I noticed that at the very first moments the app loading, status bar is really as my expected, but a moment later it becomes wrong.

Before: https://i.stack.imgur.com/i43cL.png

After: https://i.stack.imgur.com/kMwaP.png

What I've tried:

// res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.Superheroes" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:statusBarColor">@color/background_light</item>
        <item name="android:windowLightStatusBar">true</item>
        <item name="android:windowBackground">@color/background_light</item>
    </style>
</resources>
// res/values-night/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.Superheroes" parent="android:Theme.Material.NoActionBar">
        <item name="android:statusBarColor">@color/background_dark</item>
        <item name="android:windowLightStatusBar">false</item>
        <item name="android:windowBackground">@color/background_dark</item>
    </style>
</resources>
VanSuTuyDuyen
  • 335
  • 2
  • 11

4 Answers4

6

In Jetpack Compose the easiest is to use the Accompanist System UI Controller.

Add this to your dependencies : implementation "com.google.accompanist:accompanist-systemuicontroller:0.27.0"

Then, in your MainActivity:

setContent {
            MyTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val systemUiController = rememberSystemUiController()

                    SideEffect {
                        systemUiController.setStatusBarColor(
                            color = Color(0xff655D8A),
                        )
                    }
                }
            }
        }

Source and details here.

Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42
Code Poet
  • 6,222
  • 2
  • 29
  • 50
  • Thanks for your help. I'm looking for the way to do it without adding a dependency. In the comment section I actually found that! – VanSuTuyDuyen Nov 10 '22 at 14:01
6

Another way to change it without adding a dependency is at ui.theme/Theme.kt as @2801938 has mentioned:

@Composable
fun YourAppTheme(darkTheme...) {
    ...
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            window.statusBarColor = colorScheme.primary.toArgb()
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
        }
    }
    ...
}
VanSuTuyDuyen
  • 335
  • 2
  • 11
1

The compose samples project Jetchat does this through xml themes.

Be sure to cover all android versions as they do in the various themes.xml files in the same way they do or you'll end up with some weird behaviour on specific versions.

Chris
  • 4,662
  • 2
  • 19
  • 27
0
if (!view.isInEditMode) {
    SideEffect {
        val window = (view.context as Activity).window
        window.statusBarColor = colorScheme.primaryContainer.toArgb()
        WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
    }
}

Change the primaryConatiner color in Color.kt file and then change it also according to Light and Dark theme like this

private val DarkColorScheme = darkColorScheme(
primaryContainer = Color.Black,
onPrimaryContainer = Color.White
)

private val LightColorScheme = lightColorScheme(
primaryContainer = RoyalBlue,
onPrimaryContainer = Color.White
)

In Color.kt file

val RoyalBlue = Color(0xFF246EE9)