How to force close dark mode theme for Jetpack Compose

Viewed 1467

I wrote some Jetpack Compose Demo, but I found library bug about adapt dark mode, therefore I want to show light mode only in my App, however when I set <item name="android:forceDarkAllowed" tools:targetApi="q">false</item> and AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) , those do not work, any idea for show light mode only for Jetpack Compose?

2 Answers

The color we used for compose is not defined in xml, should be something like below:

@Composable
fun MyComposeTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }
    MaterialTheme(
        colorScheme = colors,
        content = content
    )
}

As you can see, you can pass any color as you wish in MaterialTheme function call, just remove the dark mode check will do the trick.

Simply add an item in both res/theme.xml and res/theme.xml(night) file is <item name="android:windowBackground">@color/white</item> in both darkand light mode it will be white.

Thank you.

Related