What you'll learn
Two things shape almost every composable: how you style it and the Modifier you attach. Modifiers are Compose's universal system for size, spacing, background, borders, click handling, and much more — learn them once and use them everywhere.
By the end you'll be able to:
- Style
Textwith colour, size, and weight - Explain what a
Modifieris and how it chains - Use the most common modifiers
- Understand why modifier order matters
Styling Text
Text accepts styling parameters directly: color, fontSize (measured in sp), fontWeight, fontStyle, and textAlign. For richer or reusable styling you can pass a whole TextStyle, and later you'll pull styles from MaterialTheme.typography:
Text(
text = "Hello Compose",
color = Color.White,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.background(Color(0xFF6366F1))
.padding(horizontal = 20.dp, vertical = 12.dp)
)Note
sp (scale-independent pixels) is for font sizes — it respects the user's system font-size setting, an accessibility win. dp (density-independent pixels) is for everything else — padding, sizes, spacing — so layouts look consistent across screen densities. Use sp for text, dp for space.The Modifier system
Almost every composable takes a modifier parameter. A Modifier is a chainable list of instructions that decorate or configure the composable — its size, padding, background, border, whether it's clickable, and more. You build one by chaining calls onto Modifier, and each call returns a new Modifier so they read top to bottom.
Key idea
modifier: Modifier = Modifier parameter in your own composables and pass it to the root element inside. This lets callers size and position your component from the outside — it's the single most important convention for writing reusable composables.Common modifiers
You'll reach for these constantly:
| Modifier | Does |
|---|---|
.padding(16.dp) | Adds space at this point in the modifier chain |
.fillMaxWidth() | Stretches to the full available width |
.size(48.dp) | Requests width and height within the parent's constraints |
.background(color) | Draws a background colour or shape |
.clip(shape) | Clips content to a shape (e.g. rounded corners) |
.clickable { } | Makes the composable respond to taps |
Order matters
Modifiers apply in the order you chain them, and that order changes the result — a common source of surprise for beginners. Compare padding before and after a background:
// padding THEN background: padding is OUTSIDE the yellow background
Modifier.padding(16.dp).background(Color.Yellow)
// background THEN padding: yellow includes the padded area around the content
Modifier.background(Color.Yellow).padding(16.dp)Watch out
.padding().background() and .background().padding() look different: in the first, the padding sits outside the background; in the second, the padding pushes the content in from the background's edge. When spacing looks off, check your modifier order first.Practice: one small working feature
Build a text card with background and padding. Swap their modifier order and compare.
Check your result
You can explain which area is colored in each version and read a long title at large font size.
Remember
Recap & quick check
Key takeaways
- Text takes styling params: color, fontSize (sp), fontWeight, fontStyle, textAlign.
- Use sp for font sizes (respects user settings) and dp for spacing and sizes.
- A Modifier is a chainable set of instructions for size, spacing, background, clicks, and more.
- Accept modifier: Modifier = Modifier in your composables and pass it to the root — key for reuse.
- Modifier order matters: .padding().background() differs from .background().padding().
Quick check
1. Which unit should you use for font sizes?
2. What is a Modifier?
3. Which modifier makes a composable stretch to the full width?
4. Why accept modifier: Modifier = Modifier in your composables?
5. Does .padding().background() look the same as .background().padding()?
Next, explore Text in depth, then add images and icons. Once you know these building blocks, we will arrange them into screens with Column, Row, and Box.