Sparsh Dutta
Sparsh Dutta

Reputation: 2980

App with multiple themes using Jetpack Compose

I want my app to provide options of having dark theme and light theme to the user, and the app's theme will become what the user selects.

How do I accomplish this with Android Jetpack Compose?

I had been going through the Android codelabs for app theming, and found the following code snippet:

@Composable
fun BasicsCodelabTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkColors
    } else {
        LightColors
    }

    MaterialTheme(colors = colors) {
        content()
    }
}

But the above code is for changing the theme corresponding to 'system' theme, not what user selects in the app.

Upvotes: 0

Views: 714

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363707

Just set the darkTheme parameter to the value defined by the user.
The value isSystemInDarkTheme() is only the default value if you don't pass the parameter and you can also change this implementation as you prefer.

Something:

setContent {
    BasicsCodelabTheme(darkTheme = true /*user choice */) {
       //... 
}

Upvotes: 2

Related Questions