Modifiers: Size, Padding, Background and Clickable
What is a Modifier?
A Modifier is a chain of instructions that changes how a composable looks and behaves.
kotlin
Text(
text = "Buy now",
color = Color.White,
modifier = Modifier
.fillMaxWidth()
.background(Color(0xFF1565C0), RoundedCornerShape(12.dp))
.clickable { println("Clicked") }
.padding(16.dp)
)
Order Matters!
kotlin
// 1. padding first: the space is OUTSIDE the yellow, so only the text area is yellow
Text("Hi", Modifier.padding(16.dp).background(Color.Yellow))
// 2. background first: the color includes the padding
Text("Hi", Modifier.background(Color.Yellow).padding(16.dp))
Read modifiers from top to bottom: Each modifier wraps what comes after it. In the first example the space is added outside the yellow, in the second the yellow includes the space. The same idea applies to
clickable and padding: put clickable first to make the padding clickable too.
Common Modifiers
| Modifier | Effect |
|---|---|
padding(16.dp) | Space around or inside |
size(100.dp), width, height | Fixed size |
fillMaxWidth(), fillMaxSize() | Fill available space (optionally a fraction) |
background(color, shape) | Colored background |
border(1.dp, color, shape) | Outline |
clip(shape) | Cut to a shape |
shadow(4.dp) | Elevation shadow |
clickable { } | Tap action |
alpha(0.5f) | Transparency |
rotate(45f) | Rotation |
offset(x, y) | Move |
aspectRatio(16f/9f) | Fixed ratio |
Make Composables Reusable: Accept a Modifier
kotlin
@Composable
fun PriceTag(amount: Double, modifier: Modifier = Modifier) { // the standard convention
Text(
text = "$" + "%.2f".format(amount),
modifier = modifier
.background(Color(0x3300AA00), CircleShape)
.padding(horizontal = 12.dp, vertical = 6.dp),
fontWeight = FontWeight.Bold
)
}
// the caller decides size and position
PriceTag(19.99, Modifier.padding(8.dp))
Custom Modifier
kotlin
fun Modifier.cardStyle() = this
.shadow(4.dp, RoundedCornerShape(16.dp))
.background(Color.White, RoundedCornerShape(16.dp))
.padding(16.dp)
Text("Hello", Modifier.cardStyle())
Convention: The first optional parameter of a composable should be
modifier: Modifier = Modifier, and it should be applied to the root element.
Conclusion
Modifiers style and control every composable. The order changes the result, and reusable composables should accept a modifier.
Quick Summary: Chain modifiers from outside to inside, remember order matters and expose modifier: Modifier = Modifier in your own composables.

