Andy Zhang
Andy Zhang

Reputation: 93

How to force close dark mode theme for Jetpack Compose

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?

Upvotes: 9

Views: 7130

Answers (3)

Macbook Air
Macbook Air

Reputation: 71

on theme.kt change this

val colors = if (darkTheme) {
    DarkColorPalette
} else {
    LightColorPalette
}

to this

val colors = LightColorPalette

Upvotes: 2

user12741503
user12741503

Reputation:

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.

Upvotes: 1

Jintin
Jintin

Reputation: 1478

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.

Upvotes: 5

Related Questions