Guides

Colors

Components never use fixed colors. They take them from the theme's ColorScheme. Read it with MaterialTheme.colorScheme.

Example

// Use a color from the theme
Text("Hi", color = MaterialTheme.colorScheme.primary)

// Change one color of one component
Button(onClick = {}, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.tertiary)) { ... }

// Change colors for the whole app
MaterialTheme(colorScheme = lightColorScheme(primary = Color(0xFF006A60))) { App() }

Use theme colors, not Color(0xFF...), inside your screens. Then dark mode and custom themes work without changes.

Color roles

A ColorScheme is a set of named colors called roles. Each role says what the color is for, not what it looks like.

The main color roles, each with its "on" color for text
The main color roles, each with its "on" color for text
val c = MaterialTheme.colorScheme
Column(Modifier.width(640.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
    Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
        Swatch("primary", c.primary, c.onPrimary, Modifier.weight(1f).height(56.dp))
        Swatch("secondary", c.secondary, c.onSecondary, Modifier.weight(1f).height(56.dp))
        Swatch("tertiary", c.tertiary, c.onTertiary, Modifier.weight(1f).height(56.dp))
        Swatch("error", c.error, c.onError, Modifier.weight(1f).height(56.dp))
    }
    Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
        Swatch("primaryContainer", c.primaryContainer, c.onPrimaryContainer, Modifier.weight(1f).height(56.dp))
        Swatch("secondaryContainer", c.secondaryContainer, c.onSecondaryContainer, Modifier.weight(1f).height(56.dp))
        Swatch("tertiaryContainer", c.tertiaryContainer, c.onTertiaryContainer, Modifier.weight(1f).height(56.dp))
        Swatch("errorContainer", c.errorContainer, c.onErrorContainer, Modifier.weight(1f).height(56.dp))
    }
    Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
        Swatch("surface", c.surface, c.onSurface, Modifier.weight(1f).height(56.dp))
        Swatch("surfaceVariant", c.surfaceVariant, c.onSurfaceVariant, Modifier.weight(1f).height(56.dp))
        Swatch("inverseSurface", c.inverseSurface, c.inverseOnSurface, Modifier.weight(1f).height(56.dp))
        Swatch("outline", c.outline, c.surface, Modifier.weight(1f).height(56.dp))
    }
}

/** One colored box with its name, in the matching "on" color. */
@Composable
fun Swatch(name: String, color: Color, onColor: Color, modifier: Modifier = Modifier) {
    Box(modifier.background(color).padding(8.dp)) {
        Text(name, color = onColor, style = MaterialTheme.typography.labelMedium)
    }
}

"On" colors

Each role has an on color: the color for text and icons placed on it. onPrimary goes on primary, onSurface goes on surface, and so on. The pairs always have enough contrast to read.

Container roles are lighter versions for big areas: primaryContainer with onPrimaryContainer.

Text color is set for you

You rarely set the color of Text or Icon. A Surface sets LocalContentColor to the matching "on" color, and Text and Icon use that.

Text and icons take the "on" color of the Surface around them
Text and icons take the "on" color of the Surface around them
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    // No color on Text or Icon: both use onPrimaryContainer.
    Surface(color = MaterialTheme.colorScheme.primaryContainer, shape = MaterialTheme.shapes.medium) {
        Row(Modifier.padding(12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Icon(Icons.Filled.Favorite, contentDescription = null)
            Text("primaryContainer")
        }
    }
    // Same code, other background: both use onTertiary.
    Surface(color = MaterialTheme.colorScheme.tertiary, shape = MaterialTheme.shapes.medium) {
        Row(Modifier.padding(12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Icon(Icons.Filled.Favorite, contentDescription = null)
            Text("tertiary")
        }
    }
}

contentColorFor finds the "on" color for a background. Components use it as the default for their contentColor parameter:

Surface(color = MaterialTheme.colorScheme.primaryContainer) {
    // Text inside is onPrimaryContainer, because
    // contentColor = contentColorFor(color) by default.
}

If you pass a color that is not in the theme, contentColorFor can't find a pair. Then it keeps the current content color. Set contentColor yourself in that case.

Surfaces and containers

Material 3 shows depth with color, not only with shadows. There are five surface containers, from lowest to highest:

Surface containers, from lowest to highest
Surface containers, from lowest to highest
val c = MaterialTheme.colorScheme
Column(Modifier.width(320.dp)) {
    Swatch("surfaceContainerLowest", c.surfaceContainerLowest, c.onSurface, Modifier.fillMaxWidth())
    Swatch("surfaceContainerLow", c.surfaceContainerLow, c.onSurface, Modifier.fillMaxWidth())
    Swatch("surfaceContainer", c.surfaceContainer, c.onSurface, Modifier.fillMaxWidth())
    Swatch("surfaceContainerHigh", c.surfaceContainerHigh, c.onSurface, Modifier.fillMaxWidth())
    Swatch("surfaceContainerHighest", c.surfaceContainerHighest, c.onSurface, Modifier.fillMaxWidth())
}

/** One colored box with its name, in the matching "on" color. */
@Composable
fun Swatch(name: String, color: Color, onColor: Color, modifier: Modifier = Modifier) {
    Box(modifier.background(color).padding(8.dp)) {
        Text(name, color = onColor, style = MaterialTheme.typography.labelMedium)
    }
}

Components pick one each. An elevated card uses surfaceContainerLow, a menu surfaceContainer, a filled card and a filled text field surfaceContainerHighest. Use them for your own boxes too, so they match.

tonalElevation on Surface does something similar: it tints surface toward the primary color. The containers are newer and give more control.

Component colors

Components with several colors take one colors object, like ButtonColors or CardColors. You don't create these directly. Each component has a function in its Defaults object that fills in the theme colors, and you change only what you pass:

ButtonDefaults.buttonColors(containerColor = ...)
CardDefaults.cardColors(containerColor = ...)
SwitchDefaults.colors(checkedTrackColor = ...)
Change one color of one component
Change one color of one component
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    Button(onClick = {}) { Text("Default") }
    Button(
        onClick = {},
        // Pick from the theme, so it still works in dark mode.
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.tertiary,
            contentColor = MaterialTheme.colorScheme.onTertiary,
        ),
    ) { Text("Tertiary") }
}

The colors object also holds the disabled colors, so a disabled button still looks right after you change it.

Your own color scheme

lightColorScheme and darkColorScheme return the baseline purple scheme. Pass only the roles you want to change:

val light = lightColorScheme(
    primary = Color(0xFF006A60),
    onPrimary = Color.White,
    primaryContainer = Color(0xFF74F8E5),
    onPrimaryContainer = Color(0xFF00201C),
)

When you change a role, change its "on" role and container too, or text can become hard to read.

A full scheme has more than 40 roles. Make one from a single seed color with the Material Theme Builder. It exports Kotlin code you can paste.

expressiveLightColorScheme() is the light scheme that MaterialExpressiveTheme uses.

On Android 12 and newer, dynamicLightColorScheme(context) builds a scheme from the user's wallpaper. It is Android only, so it is not in the common code these docs cover.

Summary

You want Write
A theme color MaterialTheme.colorScheme.primary
Text on a colored box Put it in a Surface(color = ...), it picks the "on" color
The "on" color for a background contentColorFor(background)
A background for your own box MaterialTheme.colorScheme.surfaceContainer
Change one component colors = XxxDefaults.xxxColors(...)
Change the whole app MaterialTheme(colorScheme = lightColorScheme(...))
Dark mode See the Theming guide