Guides

Modifier

Almost every composable has a modifier parameter. A Modifier changes how an element is sized, placed, drawn, and how it reacts to input, without the composable having a parameter for each.

Example

Text(
    "Hello",
    modifier = Modifier
        .padding(16.dp)          // space around it
        .fillMaxWidth()          // as wide as the parent
        .clickable { /* ... */ } // react to clicks
)

What a Modifier is

A Modifier is an ordered list of instructions. Modifier on its own is the empty list. Each call like .padding(16.dp) returns a new, longer list; nothing is changed in place.

That is why every chain starts with Modifier and is passed as one value:

val cardModifier = Modifier.fillMaxWidth().padding(8.dp)  // a value like any other
Card(modifier = cardModifier) { ... }

Modifier functions come from several libraries, but you use them the same way. Your editor imports them as you type.

Order matters

The chain is applied from left to right, outside to inside. Each modifier wraps everything after it.

The same two modifiers in a different order
The same two modifiers in a different order
Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) {
    // Padding first: the background is drawn inside the padding.
    Text(
        "padding, then background",
        Modifier.padding(16.dp).background(MaterialTheme.colorScheme.primaryContainer),
    )
    // Background first: the background also covers the padding.
    Text(
        "background, then padding",
        Modifier.background(MaterialTheme.colorScheme.primaryContainer).padding(16.dp),
    )
}

The same goes for clicks. Modifier.clickable { }.padding(16.dp) makes the padding clickable too; Modifier.padding(16.dp).clickable { } doesn't. A good rule: put clickable and clip before padding, so the click area and the ripple cover the whole element.

Size

Fixed sizes, and filling the space of the parent
Fixed sizes, and filling the space of the parent
Column(
    Modifier.width(320.dp).border(1.dp, MaterialTheme.colorScheme.outlineVariant).padding(8.dp),
    verticalArrangement = Arrangement.spacedBy(8.dp),
) {
    Block("size(64.dp)", Modifier.size(64.dp))
    Block("width(200.dp).height(40.dp)", Modifier.width(200.dp).height(40.dp))
    Block("fillMaxWidth().height(40.dp)", Modifier.fillMaxWidth().height(40.dp))
    Block("fillMaxWidth(0.5f).height(40.dp)", Modifier.fillMaxWidth(0.5f).height(40.dp))
}

/** A colored box with a label, to show sizes. */
@Composable
fun Block(label: String, modifier: Modifier) {
    Box(modifier.background(MaterialTheme.colorScheme.secondaryContainer), contentAlignment = Alignment.Center) {
        Text(label, style = MaterialTheme.typography.labelMedium)
    }
}
Modifier Does
Modifier.size, Modifier.width, Modifier.height A fixed size, in dp
Modifier.fillMaxWidth, Modifier.fillMaxHeight, Modifier.fillMaxSize As big as the parent allows. Pass a fraction, like 0.5f, for part of it
Modifier.widthIn, Modifier.heightIn, Modifier.sizeIn A smallest and largest size: widthIn(min = 120.dp, max = 400.dp)
Modifier.defaultMinSize A smallest size, only if nothing else sets one
Modifier.wrapContentSize As small as the content, even if the parent asks for more
Modifier.aspectRatio Height follows width: aspectRatio(16f / 9f)
Modifier.requiredSize A size even if the parent says it doesn't fit. Rarely needed

The parent always has the last word. A size(500.dp) inside a 300 dp wide Column gets 300 dp, unless you use requiredSize.

Space inside rows and columns

Some modifiers only exist inside a certain parent. They come from its scope, the this inside its content lambda:

weight shares the free space of a Row
weight shares the free space of a Row
Row(Modifier.width(320.dp).height(48.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    Block("72 dp", Modifier.width(72.dp).fillMaxHeight())
    Block("weight(1f)", Modifier.weight(1f).fillMaxHeight())
    Block("weight(2f)", Modifier.weight(2f).fillMaxHeight())
}

/** A colored box with a label, to show sizes. */
@Composable
fun Block(label: String, modifier: Modifier) {
    Box(modifier.background(MaterialTheme.colorScheme.secondaryContainer), contentAlignment = Alignment.Center) {
        Text(label, style = MaterialTheme.typography.labelMedium)
    }
}

If your editor doesn't offer weight, you are not directly inside a Row or Column.

Padding and position

Padding takes no colors. For space between children, prefer Arrangement.spacedBy(8.dp) on the Row or Column over padding on each child.

Looks

background, border, clip and shadow
background, border, clip and shadow
val shape = RoundedCornerShape(16.dp)
Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {
    Box(Modifier.size(64.dp).background(MaterialTheme.colorScheme.primary, shape))
    Box(Modifier.size(64.dp).border(3.dp, MaterialTheme.colorScheme.primary, shape))
    Box(Modifier.size(64.dp).clip(CircleShape).background(MaterialTheme.colorScheme.tertiary))
    Box(Modifier.size(64.dp).shadow(8.dp, shape).background(MaterialTheme.colorScheme.surface))
}
Modifier Does
Modifier.background Fills with a color or Brush, optionally in a shape
Modifier.border Draws an outline: border(1.dp, color, shape)
Modifier.clip Cuts off everything outside a Shape. Content and clicks too
Modifier.shadow A shadow in a shape, like elevation
Modifier.alpha Makes it see-through: alpha(0.5f)
Modifier.blur Blurs it

Inside Material components, use their own parameters instead, like shape and colors on a Card. They also handle the disabled and pressed states.

Transformations

offset, rotate, scale and alpha change only how it is drawn
offset, rotate, scale and alpha change only how it is drawn
Row(Modifier.padding(8.dp), horizontalArrangement = Arrangement.spacedBy(24.dp)) {
    listOf(
        Modifier.offset(x = 8.dp, y = 8.dp),
        Modifier.rotate(20f),
        Modifier.scale(1.3f),
        Modifier.alpha(0.4f),
    ).forEach { effect ->
        Icon(
            Icons.Filled.Favorite,
            contentDescription = null,
            tint = MaterialTheme.colorScheme.primary,
            modifier = Modifier.size(40.dp).then(effect),
        )
    }
}

Modifier.offset, Modifier.rotate, Modifier.scale and Modifier.alpha change how the element is drawn, not its size in the layout. Neighbors don't move.

For several at once, or values that animate every frame, use Modifier.graphicsLayer:

Modifier.graphicsLayer {
    rotationZ = angle
    scaleX = 1.2f
    alpha = 0.8f
}

Drawing your own

drawBehind draws your own shapes under the content
drawBehind draws your own shapes under the content
val dot = MaterialTheme.colorScheme.tertiaryContainer
Text(
    "Drawn behind",
    Modifier
        .padding(8.dp)
        .drawBehind {
            // size is the size of the element, in pixels.
            drawCircle(dot, radius = size.height / 2, center = Offset(size.height / 2, size.height / 2))
        }
        .padding(horizontal = 24.dp, vertical = 12.dp),
)

Inside, size is the size of the element in pixels, and you have drawCircle, drawRect, drawLine, drawPath and more.

Clicks and input

Modifier.clickable { open(item) }
Modifier.combinedClickable(onClick = { open(item) }, onLongClick = { select(item) })
Modifier.toggleable(value = checked, onValueChange = { checked = it })
Modifier.selectable(selected = isSelected, onClick = { pick(item) })

Buttons, cards and list items already have onClick. Use these modifiers for your own elements.

To know whether an element is pressed, hovered or focused, pass your own interactionSource and read it. See InteractionSource.

Scrolling

Column(Modifier.verticalScroll(rememberScrollState())) { ... }
Row(Modifier.horizontalScroll(rememberScrollState())) { ... }

Modifier.verticalScroll and Modifier.horizontalScroll make a Column or Row scroll. For long lists, use LazyColumn and LazyRow instead: they only create the items on screen.

Reading size and position

Screen readers and tests

Material's own modifiers

Writing composables that take a Modifier

Give your own composables a modifier parameter too. The convention:

  1. Name it modifier, type Modifier, default Modifier.
  2. Make it the first parameter with a default value.
  3. Apply it to the outermost element, first in the chain. Add your own modifiers after it.
  4. Apply it once only, not to children.
Your own composable with a modifier parameter
Your own composable with a modifier parameter
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
    Tag("Default")
    // The caller adds size and color; Tag keeps its own padding and shape.
    Tag("Wide and clickable", Modifier.width(240.dp).clickable { /* open */ })
}

/** Takes a Modifier and applies it first, to the outermost element. */
@Composable
fun Tag(text: String, modifier: Modifier = Modifier) {
    Text(
        text,
        modifier = modifier
            .clip(MaterialTheme.shapes.small)
            .background(MaterialTheme.colorScheme.secondaryContainer)
            .padding(horizontal = 12.dp, vertical = 6.dp),
        style = MaterialTheme.typography.labelLarge,
    )
}

Then callers can size, place and pad your composable from outside, like any Material component.

Modifiers that depend on a condition

Use then to add a modifier only sometimes:

Modifier
    .padding(8.dp)
    .then(if (isSelected) Modifier.border(2.dp, MaterialTheme.colorScheme.primary) else Modifier)

Modifier on its own adds nothing, so the else branch keeps the chain as it is.

Summary

You want Write
Space around Modifier.padding(16.dp)
A fixed size Modifier.size(48.dp)
Full width Modifier.fillMaxWidth()
Share space in a Row Modifier.weight(1f)
A colored, rounded box Modifier.clip(RoundedCornerShape(8.dp)).background(color)
An outline Modifier.border(1.dp, color, shape)
Clickable Modifier.clickable { }
Scrolling Modifier.verticalScroll(rememberScrollState())
Only sometimes .then(if (x) Modifier.border(...) else Modifier)