Guides

Theming

A theme is one place for the colors, text styles, shapes and motion of your app. Components read it, so you don't pass these values around yourself.

Example

@Composable
fun AppTheme(content: @Composable () -> Unit) {
    MaterialTheme(
        colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme(),
        typography = Typography(),
        shapes = Shapes(),
        content = content,
    )
}

// In main() or your Activity:
AppTheme { App() }

Write your own AppTheme once, and wrap your whole app in it.

Make a theme with the Theme Builder

The Material Theme Builder makes a full theme from one color or an image:

  1. Pick a source color, or upload an image to take colors from.
  2. Pick fonts for headlines and body text.
  3. Click Export and choose Jetpack Compose (Theme.kt).

You get Color.kt, Theme.kt and Type.kt with light, dark and high-contrast color schemes. They work in Compose Multiplatform too. Remove the Android-only parts, like dynamic color.

What a theme holds

Part Type Read it with Guide
Colors ColorScheme MaterialTheme.colorScheme Colors
Text styles Typography MaterialTheme.typography Typography
Corner shapes Shapes MaterialTheme.shapes Shapes
Animations MotionScheme MaterialTheme.motionScheme —

Every parameter of MaterialTheme is optional. What you leave out stays as it is: the baseline values, or the values of a theme around it.

How components find the theme

MaterialTheme(...) { } doesn't pass anything as a parameter. It puts the values in CompositionLocals: values that every composable inside can read, however deep.

MaterialTheme.colorScheme is a short way to read one of them. Defaults read them too. When you write nothing, a Button calls ButtonDefaults.buttonColors(), which reads MaterialTheme.colorScheme.primary.

So: change the theme, and every default in it changes.

Two other locals matter for everyday code:

Dark mode

Give the theme a different color scheme for dark mode. darkColorScheme is the dark version of the baseline colors.

The same button in a light and a dark color scheme
The same button in a light and a dark color scheme
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    MaterialTheme(colorScheme = lightColorScheme()) {
        Surface(shape = MaterialTheme.shapes.medium) {
            Button(onClick = {}, Modifier.padding(16.dp)) { Text("Light") }
        }
    }
    MaterialTheme(colorScheme = darkColorScheme()) {
        Surface(shape = MaterialTheme.shapes.medium) {
            Button(onClick = {}, Modifier.padding(16.dp)) { Text("Dark") }
        }
    }
}

isSystemInDarkTheme() tells you what the system uses. It works on Android, desktop, iOS and web.

Only the colors change between light and dark. Keep the same typography and shapes.

Nested themes

A MaterialTheme inside another one changes only the part of the screen inside it. Start from the current values with copy():

A nested theme changes one part of the screen
A nested theme changes one part of the screen
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    Button(onClick = {}) { Text("App theme") }
    // Copy the current colors and change one. Everything inside uses the new primary.
    MaterialTheme(colorScheme = MaterialTheme.colorScheme.copy(primary = Color(0xFF006A60))) {
        Button(onClick = {}) { Text("Nested theme") }
    }
}

Use it for a section with its own colors, like a brand area or a dark player bar on a light screen.

The Expressive theme

MaterialExpressiveTheme works like MaterialTheme, with two differences:

Expressive components work in both themes. The Expressive theme only changes the defaults.

MaterialExpressiveTheme(
    colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else expressiveLightColorScheme(),
) { App() }

Your own theme values

Material has no slot for things like spacing sizes or extra brand colors. Add your own with a CompositionLocal, the same way Material does:

Add your own theme value with a CompositionLocal
Add your own theme value with a CompositionLocal
/** Your own theme value: a spacing that is not part of Material. */
val LocalSpacing = staticCompositionLocalOf { 8.dp }

CompositionLocalProvider(LocalSpacing provides 24.dp) {
    // Anything inside can read LocalSpacing.current.
    Row(horizontalArrangement = Arrangement.spacedBy(LocalSpacing.current)) {
        OutlinedButton(onClick = {}) { Text("One") }
        OutlinedButton(onClick = {}) { Text("Two") }
    }
}
object AppTheme {
    val spacing: Dp
        @Composable get() = LocalSpacing.current
}
// AppTheme.spacing

Summary

You want Write
One theme for the app MaterialTheme(colorScheme, typography, shapes) { App() }
Dark mode colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme()
Read a value MaterialTheme.colorScheme.primary
Change part of a screen A nested MaterialTheme(colorScheme = MaterialTheme.colorScheme.copy(...))
Expressive defaults MaterialExpressiveTheme { ... }
Your own value staticCompositionLocalOf + CompositionLocalProvider