Jetpack Compose Theme — Complete Guide
In this tutorial, you'll learn about Jetpack Compose Theme. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your Compose app looks like a default Material skeleton, dark mode doesn't work, or custom colors aren't applied consistently across components.
Wrong Approach ❌
@Composable
fun BadApp() {
// No theme — default Material colors everywhere
MyApp()
}
// Custom colors hardcoded in every composable
@Composable
fun BadButton() {
Button(
onClick = {},
colors = ButtonDefaults.buttonColors(
containerColor = Color(0xFF6200EE) // Hardcoded!
)
) { Text("Click") }
}
Output: Inconsistent styling, broken dark mode, duplicated color values.
Right Approach ✅
// Custom color scheme
private val LightColors = lightColorScheme(
primary = Purple40,
secondary = PurpleGrey40,
tertiary = Pink40,
background = Color(0xFFFFFBFE)
)
private val DarkColors = darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
tertiary = Pink80,
background = Color(0xFF1C1B1F)
)
@Composable
fun DodaTechTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colorScheme = if (darkTheme) DarkColors else LightColors
MaterialTheme(
colorScheme = colorScheme,
typography = Typography(
headlineLarge = TextStyle(fontFamily = FontFamily.SansSerif, fontWeight = FontWeight.Bold),
bodyLarge = TextStyle(fontSize = 16.sp)
),
content = content
)
}
Output: Consistent theming with automatic dark mode support.
Prevention
- Wrap your app in a custom
MaterialThemewithcolorSchemeandtypography. - Define separate
lightColorSchemeanddarkColorSchemefor dark mode. - Use
MaterialTheme.colorScheme.primaryinstead of hardcoded colors. - Define
Typographyonce — override per-component when needed.
Common Mistakes with compose theme
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro