This lesson demonstrates comprehensive theming in Android with Jetpack Compose using Material 3 Design.
See the app in action! Below are screenshots showing the complete implementation of Material 3 theming:
Light theme showcasing the complete typography hierarchy (Display, Headline, Title, Body, Label) and Material 3 color palette with proper contrast ratios.
Light theme displaying various Material 3 components including Filled, Tonal, Outlined, Elevated, and Text buttons, along with Card variations and interactive controls.
Dark theme with adjusted color scheme showing how all typography styles and colors automatically adapt for optimal readability in low-light conditions.
Dark theme demonstrating that all Material 3 components seamlessly transition to dark mode with appropriate surface colors and contrast preservation.
- Theme Toggle: Sun/Moon icon in the top bar switches between light and dark modes
- Color Adaptation: Every component automatically uses theme-appropriate colors
- Typography Hierarchy: Clear visual distinction between Display, Headline, Title, Body, and Label styles
- Component Variety: Multiple button styles, cards, switches, checkboxes, and chips all respecting the theme
- Contrast & Accessibility: Proper "on" colors ensure text remains readable on all backgrounds
- Smooth Transitions: All UI elements update instantly when toggling themes
- Complete light and dark color schemes
- Primary, Secondary, Tertiary colors and their variants
- Surface, Background, and Error colors
- Proper "on" colors for contrast (onPrimary, onSecondary, etc.)
- Display styles (Large, Medium, Small) - for the largest text
- Headline styles (Large, Medium, Small) - for section headers
- Title styles (Large, Medium, Small) - for card titles and labels
- Body styles (Large, Medium, Small) - for content text
- Label styles (Large, Medium, Small) - for small captions
- Dark mode toggle with a switch button
- Smooth transitions between themes
- Persistent theme state using
remember
- Status bar appearance changes based on theme
- Buttons: Filled, Tonal, Outlined, Elevated, Text
- Cards: Filled and Outlined
- Selection controls: Switch, Checkbox
- Chips: Assist, Filter, Suggestion
Material 3 uses a comprehensive color system:
Primary - Main brand color
Secondary - Accent color for less prominent elements
Tertiary - Additional accent for variety
Error - For error states and warnings
Surface - Component backgrounds
Background - Screen background
Each color has a corresponding "on" color for text/icons that appear on top of it.
Material 3 provides 15 text styles organized by purpose:
- Display: Hero text (57sp, 45sp, 36sp)
- Headline: Section headers (32sp, 28sp, 24sp)
- Title: Subsections and cards (22sp, 16sp, 14sp)
- Body: Main content (16sp, 14sp, 12sp)
- Label: Small text and captions (14sp, 12sp, 11sp)
- Color.kt: Defines all color values
- Type.kt: Defines typography styles
- Theme.kt: Combines colors and typography into MaterialTheme
- MainActivity.kt: Uses the theme and provides toggle functionality
- Toggle button in the app bar
- Icon changes based on current theme (sun/moon)
- Entire UI updates reactively
- Demonstrates all major text styles
- Shows proper hierarchy and sizing
- Uses semantic styling (headline for headers, body for content)
- Visual representation of each color role
- Shows proper contrast between background and foreground
- Demonstrates Material 3 color tokens
- Various button styles with theme-aware colors
- Different card types
- Interactive controls (switches, checkboxes)
- Modern chip components
- Use Theme Colors: Always use
MaterialTheme.colorScheme.*
instead of hardcoded colors - Use Typography Styles: Use
MaterialTheme.typography.*
instead of manual text styling - Semantic Naming: Use colors by role (primary, error) not by appearance (red, blue)
- Accessibility: Proper contrast ratios with "on" colors
- Consistency: All components automatically adapt to theme changes
AndroidKotlinLesson7Theme(darkTheme = isDarkTheme) {
// Your composables here
}
Text(
text = "Hello",
color = MaterialTheme.colorScheme.primary
)
Text(
text = "Title",
style = MaterialTheme.typography.headlineMedium
)
Card(
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
)
) {
// Card content
}
- Open the project in Android Studio
- Run the app on an emulator or device
- Tap the sun/moon icon in the top bar to toggle between light and dark themes
- Scroll through the app to see all themed components
- Material 3 provides a comprehensive design system
- Themes should be defined once and applied consistently
- Runtime theme switching is simple with state management
- All Material components automatically respect the theme
- Proper color contrast and typography hierarchy improve UX
- Explore custom color schemes with Material Theme Builder
- Learn about dynamic color (Android 12+) which adapts to wallpaper
- Study accessibility guidelines for color contrast
- Implement theme persistence with DataStore
- Create custom typography with different font families