How to prevent white background when app starts up in Jetpack compose

I have created an app with Jetpack compose and expected the start up background is black or some other colors, not white. This is my themes.xml

<style name="Theme.AlluringScreenshot" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="android:statusBarColor">#030318</item>
    <item name="android:windowBackground">#030318</item>
</style>

The above style works well till on Android 11 but Android 12. My app still has white background at starting up. Any suggestions for this matter?

Upvotes: 0

Views: 2182

Answers (3)

zjmo
zjmo

Reputation: 675

There must be some conflicting declaration.

Import material 3

implementation 'androidx.compose.material3:material3:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.8.0-alpha02'

use in parent for the activity theme:

<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
...
</style>

create a composable wrapper for the theme in Theme.kt for compose:

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme

@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    MaterialTheme(
        colorScheme = when (darkTheme) {
            true -> darkColorScheme()
            else -> lightColorScheme()
        },
        typography = Typography(),
        content = content
    )
}

and then wrap your composable screens with :

AppTheme{
    MyComposableScreen()
}

if you have to change colors do it in darkColorScheme() nad lightColorScheme(), if it is not enough then change the xml attributes

Upvotes: 0

This is the author of this question.

After spending more time diving into some documents, I have found the spot for this matter. It looks very simple than any the solutions I found. The android:windowBackground attribute only work till Android 11. This should be replaced by android:windowSplashScreenBackground to work well on Android 12.

That's all.

Upvotes: 0

Renz Salanga
Renz Salanga

Reputation: 499

Use the splash screen API as a placeholder while jetpack compose is still processing

Upvotes: 2

Related Questions