Material Theme customization lets you change colors, fonts, and shapes in your app. This helps your app look unique and match your brand.
Material Theme customization in Android Kotlin
val colors = lightColorScheme(
primary = Color(0xFF6200EE),
secondary = Color(0xFF03DAC6),
background = Color(0xFFFFFFFF)
)
MaterialTheme(
colorScheme = colors,
typography = Typography(),
shapes = Shapes(),
content = { /* Your UI here */ }
)Use lightColorScheme or darkColorScheme to define colors.
Pass your custom colors, typography, and shapes to MaterialTheme.
val customColors = lightColorScheme(
primary = Color(0xFFBB86FC),
secondary = Color(0xFF03DAC5),
background = Color(0xFFF0EAE2)
)
MaterialTheme(colorScheme = customColors) {
Text("Hello with custom colors")
}val customShapes = Shapes(
small = RoundedCornerShape(8.dp),
medium = RoundedCornerShape(16.dp),
large = RoundedCornerShape(0.dp)
)
MaterialTheme(shapes = customShapes) {
Button(onClick = {}) {
Text("Rounded Button")
}
}This app uses a custom color scheme and shapes. The background is light gray, the title text is purple, and the button has rounded corners.
import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp @Composable fun CustomThemeApp() { val customColors = lightColorScheme( primary = Color(0xFF6200EE), secondary = Color(0xFF03DAC6), background = Color(0xFFF0F0F0) ) val customShapes = Shapes( small = RoundedCornerShape(12.dp), medium = RoundedCornerShape(24.dp), large = RoundedCornerShape(0.dp) ) MaterialTheme( colorScheme = customColors, shapes = customShapes ) { Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { Column( modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp) ) { Text("Welcome to Custom Theme!", style = MaterialTheme.typography.headlineMedium, color = MaterialTheme.colorScheme.primary) Button(onClick = {}, shape = MaterialTheme.shapes.medium) { Text("Click Me") } } } } }
Always define colors for both light and dark themes for better user experience.
Use MaterialTheme to wrap your UI so all components use your custom styles.
Shapes affect buttons, cards, and other surfaces. Adjust them to match your design.
Material Theme customization changes colors, fonts, and shapes in your app.
Use colorScheme, typography, and shapes to customize.
Wrap your UI in MaterialTheme to apply your custom styles everywhere.
Practice
MaterialTheme in an Android app?Solution
Step 1: Understand MaterialTheme role
TheMaterialThemecontrols the look and feel of the app by setting colors, typography, and shapes.Step 2: Identify customization purpose
CustomizingMaterialThemechanges these visual styles globally for consistency.Final Answer:
To change the app's colors, fonts, and shapes globally -> Option BQuick Check:
MaterialTheme customization = change colors, fonts, shapes [OK]
- Confusing theme customization with adding app features
- Thinking it manages app permissions
- Assuming it handles network or data logic
MaterialTheme in Kotlin?Solution
Step 1: Recall MaterialTheme parameters
The correct parameter for colors in MaterialTheme iscolorScheme.Step 2: Match correct syntax
MaterialTheme(colorScheme = myColors) { /* UI content */ } usescolorScheme = myColors, which is the correct syntax in Kotlin.Final Answer:
MaterialTheme(colorScheme = myColors) { /* UI content */ } -> Option AQuick Check:
Use colorScheme parameter for colors [OK]
- Using 'colors' instead of 'colorScheme'
- Using non-existent parameters like 'setColors' or 'applyColors'
- Forgetting to wrap UI content inside the theme block
Text component display if primary in colorScheme is set to Color.Red?
MaterialTheme(
colorScheme = ColorScheme(
primary = Color.Red,
secondary = Color.Blue
)
) {
Text("Hello", color = MaterialTheme.colorScheme.primary)
}Solution
Step 1: Identify colorScheme primary value
Theprimarycolor incolorSchemeis set toColor.Red.Step 2: Check Text color usage
TheTextcolor usesMaterialTheme.colorScheme.primary, so it will beColor.Red.Final Answer:
Red -> Option CQuick Check:
Text color = primary color = Red [OK]
- Confusing primary with secondary color
- Assuming default color instead of theme color
- Mixing up color names
MaterialTheme(
typography = Typography(
bodyLarge = TextStyle(fontSize = 20)
)
) {
Text("Welcome")
}Solution
Step 1: Check TextStyle fontSize usage
ThefontSizemust include a unit like20.sp, not just20.Step 2: Identify correct syntax
Without the unit, the code will cause a type error because fontSize expects aTextUnit.Final Answer:
TextStyle requires a unit for fontSize, like sp or dp -> Option AQuick Check:
fontSize needs unit like sp [OK]
- Omitting units for fontSize
- Thinking typography can't be customized
- Expecting Text to need explicit typography parameter
Solution
Step 1: Understand shape categories in MaterialTheme
MaterialTheme defines shapes assmall,medium, andlarge. Buttons use themediumshape by default.Step 2: Apply rounded corners to medium shape
Settingmedium = RoundedCornerShape(16.dp)customizes button corners.Final Answer:
MaterialTheme(shapes = Shapes(medium = RoundedCornerShape(16.dp))) { Button(...) } -> Option DQuick Check:
Buttons use medium shape slot [OK]
- Changing small or large shapes instead of medium
- Using non-existent shape categories like extraLarge
- Forgetting to wrap Button inside MaterialTheme
Ready to prove it?
Pass the test and this pattern moves unknown
