Set background color for the whole app. Android, Jetpack Compose

Viewed 3775

I need to set background color for the whole app. In xml, we use android:background tag in fragment or activity.

What analog Compose has? Surface argument for theme colorPalette doesn't help. Look for your solutions.

2 Answers

You can place your app inside a Box with needed background:

setContent {
    YourTheme {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Yellow)
        ) {
            YourApp()
        }
    }
}

Put a window background color from your theme, which will set the background of your whole app, and also remove the white flicker while the app is loading (which looks quite odd if you have a color background in your app):

res/values/themes/themes.xml

<style name="Theme.YourThemeName" 
    parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ...
        <item name="android:windowBackground">@color/purple_700</item>
</style>
Related