In Jetpack Compose Material 3, how do you choose between dynamic color and a brand ColorScheme in light and dark, and what changes below Android 12?
answer
- wallpaper-derived palette
- one theme function decides
- API guard before the call
- brand fallback is your branch
basics
~20 sThe app's theme function picks the scheme: on Android 12 (API 31) and up, dynamicDarkColorScheme or dynamicLightColorScheme from the wallpaper if wanted; otherwise brand darkColorScheme or lightColorScheme, chosen with isSystemInDarkTheme(). Below Android 12 only the brand schemes exist.
solid answer
~40 sThe selection lives in one theme composable. It takes `darkTheme: Boolean = isSystemInDarkTheme()` so an in-app override can force a mode, and a `dynamicColor` flag. If dynamic color is wanted and `Build.VERSION.SDK_INT >= Build.VERSION_CODES.S`, it calls `dynamicDarkColorScheme(context)` or `dynamicLightColorScheme(context)`, which derive the scheme from the user's wallpaper; otherwise it uses the brand `darkColorScheme(...)` or `lightColorScheme(...)`, then passes the result to `MaterialTheme`. Both dynamic functions are `@RequiresApi(Build.VERSION_CODES.S)`, so below Android 12 lint flags an unguarded call and the library does not fall back for you: your own `when` branch is the fallback. Dynamic color only builds the `ColorScheme`; custom tokens and literals are untouched by it.
code
kotlin · 25 linesprivate val BankLightColors = lightColorScheme(
primary = Color(0xFF0B5CAD),
onPrimary = Color.White,
)
private val BankDarkColors = darkColorScheme(
primary = Color(0xFF9CC8FF),
onPrimary = Color(0xFF00315C),
)
@Composable
fun BankTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
dynamicColor: Boolean = false,
content: @Composable () -> Unit,
) {
val context = LocalContext.current
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
darkTheme -> BankDarkColors
else -> BankLightColors
}
MaterialTheme(colorScheme = colorScheme, content = content)
}go deeper
Recall the four scheme builders, that dynamic color needs Android 12, and that isSystemInDarkTheme() reports the system setting.
Write the theme function's selection with the SDK guard, explain why the fallback branch is yours, and why screens read colorScheme instead of the setting.
Plan for an in-app dark override, test brand schemes on pre-12 devices, and check custom tokens against wallpaper-derived surfaces.
Decide whether dynamic color is on, opt-in or off per flow, weighing system integration against brand recognition.
## The building blocks Jetpack Compose's Material 3 library gives you four functions that build a **`ColorScheme`**, plus one that reports the system setting: | Function | Source of colors | Availability | |---|---|---| | `lightColorScheme(...)` | your brand values, baseline defaults for the rest | every API level | | `darkColorScheme(...)` | your brand values for dark | every API level | | `dynamicLightColorScheme(context)` | the user's wallpaper, light variant | `@RequiresApi(Build.VERSION_CODES.S)`: API 31, Android 12 | | `dynamicDarkColorScheme(context)` | the user's wallpaper, dark variant | API 31, Android 12 | | `isSystemInDarkTheme()` | the system's dark theme setting | every API level | **Dynamic color** is the Material You feature in which the system derives a palette from the user's wallpaper; when the wallpaper changes, the scheme the dynamic functions return changes with it. Since Material 3 1.2.0 the dynamic schemes pull from the system-defined color roles, which improved contrast. `isSystemInDarkTheme()` lives in Compose Foundation and simply returns whether the system is in dark theme; it chooses nothing by itself. ## Choosing the scheme in the theme function The selection belongs in **one place**: your app's theme composable, which then passes the result to `MaterialTheme`. The documented pattern is a `when` with an API guard: 1. Decide `darkTheme`, by default from `isSystemInDarkTheme()`, but as a parameter so an in-app override can force light or dark. 2. If dynamic color is wanted **and** `Build.VERSION.SDK_INT >= Build.VERSION_CODES.S`, call `dynamicDarkColorScheme(context)` or `dynamicLightColorScheme(context)` with `LocalContext.current`. 3. Otherwise use the brand `darkColorScheme(...)` or `lightColorScheme(...)` defined once at file level. 4. Pass the chosen scheme to `MaterialTheme(colorScheme = ...)`. Components and screens below never call `isSystemInDarkTheme()` themselves; they read `MaterialTheme.colorScheme`. The foundation library's own documentation recommends reading the system setting at the top level for exactly this reason: it keeps screens unaware of the setting and makes a user override easy. ## What changes below Android 12 Below API 31 there is no wallpaper-derived system palette to read, and the two dynamic functions carry `@RequiresApi(Build.VERSION_CODES.S)`. With a `minSdk` under 31, Android lint flags an unguarded call. Material does **not** fall back to your brand scheme for you; the fallback is your own `when` branch. On those devices the app always shows the brand `lightColorScheme` or `darkColorScheme`, which is why the brand schemes must be complete for both modes even if most of your users get dynamic color. ## The brand question in a banking app A banking app with a strong brand palette has a real choice: - **Dynamic color on**: the app matches the user's wallpaper and system UI, but the brand's primary blue may be replaced with a wallpaper-derived green on the very buttons that carry the brand. - **Dynamic color off or opt-in**: the brand stays recognisable everywhere; the app looks less integrated on Android 12 and later. - **Partial**: a `dynamicColor` parameter on the theme function lets a team enable it for some flows and keep brand colors elsewhere by nesting a `MaterialTheme` with the brand scheme. Whatever you pick, remember that dynamic color only builds the `ColorScheme`. Custom tokens you provide yourself through a `CompositionLocal` (a "positive amount" green, a "caution" yellow) are untouched by it, so check that they still contrast with a wallpaper-derived surface. ## Testing the combinations The theme function has more branches than it looks, so test each one deliberately: - light and dark brand schemes on a device or emulator below Android 12, where they are the only path; - light and dark dynamic schemes on Android 12 or later, with at least two very different wallpapers; - the in-app override, if the app has one, forcing each mode while the system is in the other; - previews rendered with `darkTheme = true` and `false` passed explicitly, since a preview cannot show a real user's wallpaper palette. Contrast problems often show up in only one cell of this matrix, typically a custom token on a wallpaper-derived surface. ## Common mistakes - Calling `dynamicLightColorScheme` without an SDK guard and relying on the library to fall back. - Using only `lightColorScheme` and expecting dark theme to work because the device is in dark mode. - Reading `isSystemInDarkTheme()` inside every component instead of choosing the scheme once. - Forgetting that a user's in-app "always dark" choice needs the theme function's `darkTheme` parameter, not a second call to the system setting. - Assuming dynamic color recolors every color in the app, including custom tokens and hardcoded literals.
- How would you support an in-app 'always dark' setting alongside the system setting?Keep `isSystemInDarkTheme()` only as the default of the theme function's `darkTheme` parameter. Read the user's stored preference at the top of the UI and pass `darkTheme = when (pref) { DARK -> true; LIGHT -> false; SYSTEM -> isSystemInDarkTheme() }`. Screens keep reading `MaterialTheme.colorScheme` and never see the setting.
- Why might a bank keep dynamic color off on its main flows?The dynamic schemes replace the brand's primary and container colors with wallpaper-derived ones, so the brand blue on the main call-to-action may become green. Many brands keep their scheme on those flows, or expose a `dynamicColor` flag and nest a brand `MaterialTheme` where recognisability matters.
- Does turning on dynamic color recolor custom tokens such as a positive-amount green?No. `dynamicLightColorScheme` and `dynamicDarkColorScheme` only return a `ColorScheme` for `MaterialTheme`. Tokens you provide through your own `CompositionLocal` keep their values, so check their contrast against wallpaper-derived surfaces in both modes.
- Dynamic color works on every Android version that Compose supports.
- Below Android 12, Material automatically falls back to the app's brand scheme.
- isSystemInDarkTheme switches the app to dark colors without choosing a scheme.
- Every component should call isSystemInDarkTheme to pick its own colors.
- Dynamic color also recolors custom brand tokens provided through a CompositionLocal.
Assumes Compose 1.12 with Material 3 1.4; dynamic schemes need API 31, and since Material 3 1.2.0 they pull from system-defined color roles.