CPS251
Android Development
Customizing Material Components
Introduction
Sometimes you want a button, card, or other Material component to look a little different—maybe a warning button, a special card, or a unique color for a certain action. In Jetpack Compose, you can customize individual Material components without changing your whole app's theme.
When to Customize Material Components
- You want to highlight a specific action (like a danger or success button)
- You want to make one card or button stand out from the rest
- You want to experiment with different looks for certain UI elements
- You want to match a brand color for a special feature
Common Customizations
| Component | What You Can Change | How |
|---|---|---|
| Button | Colors, shape, elevation | Use colors, shape, elevation parameters |
| Card | Colors, shape, border | Use colors, shape, border parameters |
| TextField | Colors, shape | Use colors, shape parameters |
Practical Example
@Composable
fun CustomButtonExample() {
Button(
onClick = { /* Do something */ },
colors = ButtonDefaults.buttonColors(containerColor = Color.Red),
shape = RoundedCornerShape(50),
elevation = ButtonDefaults.buttonElevation(12.dp)
) {
Text("Danger!", color = Color.White)
}
}
- This button uses a red background, pill shape, and higher elevation for emphasis
- You can customize any Material component in a similar way
How this example renders
Above is just a snippet of the code to view the full code, you need to go to my GitHub page and look at the chapter13 custom.kt file. When you run the code it will look like the example below.
Tips for Success
- Use custom styles sparingly—too many can make your app look messy
- Keep important actions (like delete) visually distinct
- Test custom components in both light and dark mode
Common Mistakes to Avoid
- Overusing custom styles so nothing stands out
- Using colors that clash with your theme
- Forgetting about accessibility (contrast, touch size)
Best Practices
- Base most of your UI on the theme, and only customize when needed
- Document why you use custom styles
- Keep customizations consistent across your app