Material 3 theming

Learn Material 3 theming in Jetpack Compose to implement colorScheme, typography, and shapes using CompositionLocal and dynamic color for Android. About 25 minutes.

Why it matters

  • Every screen in a real app reads colors, type, and shapes from the theme. Hardcoding any of them breaks dark mode or dynamic color and creates visual bugs that are hard to track down.
  • Interviewers ask how theming propagates through the composition tree without passing colors as parameters. Knowing CompositionLocal answers that directly.
  • Dynamic color (Material You) is available on Android 12+, but an app only gets it if its theme opts in. Supporting it, with a fallback scheme for older devices, is now the default in the Android Studio Compose templates.
  • A single theme function at the app root replaces dozens of per-screen color constants. This is the mechanism that makes a large app stay visually consistent with one change.

The simple explanation

  • Think of MaterialTheme as a uniform policy for a building.
  • The policy says: walls are this color, doors have this radius, signs use this font.
  • Every room (composable) reads the policy from a shared board (CompositionLocal) instead of painting itself independently.
  • When the building switches to night mode, the board updates once; every room repaints automatically.
  • Dynamic color is like letting the building's exterior (wallpaper) influence the interior palette.
  • If one room ignores the board and paints itself a fixed blue, it looks wrong at night.

Material3 exposes three theme objects: ColorScheme, Typography, and Shapes. You pass them into the MaterialTheme composable at the root of your composition. Internally, MaterialTheme wraps its children in CompositionLocalProvider, setting the (internal) LocalColorScheme, LocalTypography, and LocalShapes.

Any composable below the MaterialTheme call reads values through MaterialTheme.colorScheme, MaterialTheme.typography, or MaterialTheme.shapes. These are CompositionLocal reads, so nobody has to pass colors down as parameters. When the theme changes (for example light to dark), composables that read the theme recompose with the new values. Theme changes are rare, so this costs little.

ObjectKey propertiesTypical use
ColorSchemeprimary, onPrimary, surface, onSurface, surfaceVariant, error, outlinebackgrounds, text, borders
TypographydisplayLarge, headlineMedium, titleLarge, bodyLarge, labelMediumtext styles on Text
ShapesextraSmall, small, medium, large, extraLargecorner radius on Card, FAB, dialogs, sheets

Dynamic color (Android 12+, API 31) derives a ColorScheme from the user's wallpaper. You call dynamicLightColorScheme(context) or dynamicDarkColorScheme(context). On older devices you fall back to lightColorScheme() / darkColorScheme(). The check uses Build.VERSION.SDK_INT >= Build.VERSION_CODES.S.

isSystemInDarkTheme() (from androidx.compose.foundation) reads the system dark-mode flag. You pass its result into your theme function so the correct scheme is selected before any child composable runs.

A common mistake is wrapping a screen in a second theme call that sets its own scheme, such as MaterialTheme(colorScheme = lightColorScheme()). That creates a nested theme scope that shadows the app theme for that subtree, so the screen stays light in dark mode. Keep your app theme at the root, and nest a theme only when you deliberately want to override a subtree.

Notes

  • MaterialTheme is a composable that sets three CompositionLocal values: LocalColorScheme, LocalTypography, LocalShapes. Children read them via MaterialTheme.colorScheme, .typography, .shapes.
  • MaterialTheme parameters default to the current theme values, so a nested MaterialTheme(shapes = ...) changes only shapes and inherits the rest from the outer theme.
  • Dynamic color APIs (dynamicLightColorScheme, dynamicDarkColorScheme) require API 31 (Android 12). Always provide a lightColorScheme() / darkColorScheme() fallback for older devices.
  • isSystemInDarkTheme() lives in androidx.compose.foundation. It reads the system dark-mode preference and returns a Boolean.
  • Customise Typography or Shapes with named arguments (Typography(titleLarge = ...)) or .copy(). Every role you do not pass keeps its Material 3 default.
  • Place your app theme at the setContent root. A nested MaterialTheme that passes its own colorScheme shadows the outer one for that subtree, which is a bug unless you meant it.
  • ColorScheme roles follow a pattern: primary / onPrimary, surface / onSurface, surfaceVariant / onSurfaceVariant, error / onError. The on* role is the text/icon color that contrasts with its base.
  • Shapes has five slots: extraSmall (4dp), small (8dp), medium (12dp), large (16dp), extraLarge (28dp) by default. Card uses medium, FloatingActionButton uses large, AlertDialog uses extraLarge. Buttons use a fully rounded shape that is not one of the five slots.
  • In Material3 the accessor is MaterialTheme.colorScheme, not MaterialTheme.colors (that was Material2). If the Material2 library is also on the classpath and you import its MaterialTheme by mistake, the code compiles but reads Material2 default colors, not your theme.
  • Dynamic color reads system color resources that are derived from the wallpaper. When the user changes wallpaper colors, the system updates those resources and normally recreates running activities, so the theme picks up the new scheme without extra code.

Code example

This shows a single AppTheme function that selects light, dark, or dynamic color, customises two shape slots, and a GreetingCard that reads every value from the theme instead of hardcoding anything.

// Theme.kt – one theme function, called once in setContent
@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit,
) {
    // Pick the right ColorScheme; fall back for pre-Android 12
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
            if (darkTheme) dynamicDarkColorScheme(LocalContext.current)
            else dynamicLightColorScheme(LocalContext.current)
        darkTheme -> darkColorScheme()
        else -> lightColorScheme()
    }
 
    // Override only the slots you need; the rest keep Material3 defaults
    val shapes = Shapes(
        medium = RoundedCornerShape(16.dp),
        large = RoundedCornerShape(24.dp),
    )
 
    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography(),          // default M3 type scale
        shapes = shapes,
        content = content,
    )
}
 
// A screen composable that reads everything from the theme
@Composable
fun GreetingCard(name: String) {
    Card(
        shape = MaterialTheme.shapes.medium,
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surfaceVariant,
        ),
    ) {
        Text(
            text = "Hello, " + name,
            style = MaterialTheme.typography.titleLarge,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            modifier = Modifier.padding(16.dp),
        )
    }
}
  • The when block guarantees a ColorScheme for every API level; the dynamic branch is guarded by Build.VERSION.SDK_INT >= Build.VERSION_CODES.S.
  • Shapes(medium = ..., large = ...) uses named parameters so the other three slots (extraSmall, small, extraLarge) keep their Material3 defaults.
  • GreetingCard never references a raw Color or Dp value; it reads MaterialTheme.colorScheme.surfaceVariant and MaterialTheme.shapes.medium, so it adapts to light, dark, and dynamic modes automatically.

Run the idea in plain Kotlin

fun main() {
  // Simulating theme logic without Android dependencies
  val isDark = true
  val useDynamic = true
  val sdkInt = 31 // Android 12

  val scheme = when {
      useDynamic && sdkInt >= 31 -> "Dynamic"
      isDark -> "Dark"
      else -> "Light"
  }

  val result = "Selected scheme: " + scheme
  println(result)
}

Expected output: Selected scheme: Dynamic. Change sdkInt to 30 and it prints Selected scheme: Dark.

In production

Dark-mode text invisible on a subset of screens
  • Users reported that two onboarding screens showed white text on a white background after switching to dark mode.
  • The rest of the app adapted correctly, so the bug was not in the theme function itself.
  • The two screens used Color.White and Color(0xFF1A1A1A) literals instead of colorScheme.onSurface and colorScheme.surface.
The fix
  • The team grepped the codebase for Color. literals outside the theme file and found several occurrences across multiple screens.
  • Each literal was replaced with the matching MaterialTheme.colorScheme role (e.g. onSurface, surfaceVariant).
  • A lint check was added to the CI pipeline that flags any Color( or Color.White / Color.Black usage outside Theme.kt.
  • After the fix, dark-mode screenshots in the visual-regression suite matched the light-mode layout with correct contrast.
Dynamic color ignored on Android 12 devices
  • Users on Pixel 6 (Android 12) saw the default purple Material3 baseline palette instead of the wallpaper-derived colors.
  • The theme function called lightColorScheme() unconditionally; the dynamic branch was behind a feature flag that was never enabled in production.
  • No crash or log appeared, so the issue was only visible in user screenshots.
The fix
  • The team reproduced the issue on a Pixel 6 emulator with a colorful wallpaper and confirmed the app used the static baseline scheme.
  • The feature flag was removed; the when block now checks Build.VERSION.SDK_INT >= Build.VERSION_CODES.S and calls dynamicLightColorScheme / dynamicDarkColorScheme.
  • A screenshot test on an API 31+ emulator with a non-default wallpaper was added, so a missing dynamic branch shows up as a diff.
  • The fix shipped in the next release; user screenshots in the feedback channel showed the new wallpaper-tinted palette.

Common mistakes

  • Hardcoding Color(0xFF6200EE) or Color.White in a screen composable. The color does not change with dark mode or dynamic color, so the UI looks broken in one mode. Always read from MaterialTheme.colorScheme roles.
  • Wrapping a screen in a theme call with a fixed scheme. A nested MaterialTheme(colorScheme = lightColorScheme()) creates a second CompositionLocal scope that shadows the app theme. Children of the inner theme ignore dark mode and dynamic color. Keep the app theme at the root.
  • Omitting the Build.VERSION.SDK_INT guard before calling dynamicLightColorScheme. The dynamic APIs are not available on pre-Android 12 devices. Without the guard, the app crashes on API 30 and below.
  • Overriding a type role with a bare TextStyle(fontSize = 30.sp). The new style loses the default line height, letter spacing and weight of that role. Start from the default style instead: Typography().headlineMedium.copy(fontSize = 30.sp).
  • Using MaterialTheme.colors (Material2 accessor) in a Material3 project. MaterialTheme.colors does not exist in androidx.compose.material3. If Material2 is also a dependency, the wrong import compiles and silently returns Material2 default colors. Use androidx.compose.material3.MaterialTheme.colorScheme.
  • Painting a background with Modifier.background(colorScheme.surface) instead of using Surface. Surface also sets LocalContentColor to the matching on* color, so Text and Icon inside get the right contrast. A plain background does not, and text can end up with the wrong color in dark mode.

Interview questions

Answer out loud first. Then reveal the answer and compare it at each depth.

“How does MaterialTheme make its values available to child composables?”

“What is dynamic color and how do you implement it correctly?”

“A screen shows white text on a white background in dark mode. How do you debug it?”

“How do you customise typography or shapes without breaking the rest of the Material3 defaults?”

Quiz

What mechanism does MaterialTheme use to make colorScheme, typography, and shapes available to child composables?

On which minimum Android version are dynamicLightColorScheme and dynamicDarkColorScheme available?

Text inside a Box with Modifier.background(MaterialTheme.colorScheme.surface) has the wrong color in dark mode. What is the best fix?

What is the correct way to override one typography style in Material3?

0 of 4 answered

Mini task

Add dynamic color to your app's theme (15 min)

  1. Open your existing theme file (or create Theme.kt). Write an AppTheme composable that takes darkTheme: Boolean = isSystemInDarkTheme() and dynamicColor: Boolean = true.

  2. Inside AppTheme, build a when block that returns dynamicDarkColorScheme(LocalContext.current) or dynamicLightColorScheme(LocalContext.current) when dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S, and falls back to darkColorScheme() / lightColorScheme() otherwise.

  3. Pass the selected colorScheme into MaterialTheme. Also pass Typography() and a Shapes object that overrides medium to RoundedCornerShape(16.dp).

  4. In your MainActivity.setContent, replace any existing theme call with AppTheme { /* your root composable */ }.

  5. Add a Card with shape = MaterialTheme.shapes.medium and containerColor = MaterialTheme.colorScheme.surfaceVariant inside the root composable. Put a Text with style = MaterialTheme.typography.titleLarge and color = MaterialTheme.colorScheme.onSurfaceVariant inside the card.

  6. Run the app in light mode, dark mode, and (on an Android 12+ device or emulator) with a colorful wallpaper. Confirm the card background and text color change with each mode.

Done when: the card adapts its colors in all three modes, no Color.White or Color(0xFF...) literals appear in the screen composable, and the app does not crash on an API 30 emulator.

Next: Navigation in Compose