Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 104 ยท Jetpack Compose Fundamentals

Modifiers

Learn how Compose modifiers chain, why their order changes the result, the difference between padding and size, clickable areas, graphicsLayer and custom modifiers.

A modifier changes how a composable looks or behaves without changing what it is. Size, padding, background, borders, click handling, scrolling, accessibility information and drawing effects are all modifiers. You attach them to a composable with the modifier parameter and chain several together with dots:

Kotlin
// read-only example: needs Android and Compose
Text(
    text = "Save",
    modifier = Modifier
        .padding(16.dp)
        .background(Color.LightGray)
        .clickable { save() },
)

The surprise for newcomers is that the order of the chain matters. This lesson explains why, and covers the modifiers you will use daily, including a look at graphicsLayer and how to write your own. The real Compose code is read-only because it needs the Android SDK. The runnable TryIt blocks are plain Kotlin stand-ins that evaluate a modifier chain with the same rules, using constraints (see Row, Column, Box and Arrangements) so you can see order effects as pictures made of characters.

Modifiers are an ordered chain

Modifier is an immutable list of elements. Modifier.padding(16.dp) returns a new modifier that contains the padding element, and .background(...) adds another. The chain is applied from the outside in: the first modifier wraps everything that comes after it, like layers of an onion, with the composable itself at the center.

Consider these two chains:

Kotlin
// read-only example: needs Android and Compose
Modifier.background(Color.Yellow).padding(20.dp)   // yellow includes the padding
Modifier.padding(20.dp).background(Color.Yellow)   // yellow stops inside the padding

In the first, the background is applied first, so it colors the whole box including the space that padding adds afterwards. In the second, the padding shrinks the area first and the background only covers what is left. The same applies to sizes, borders, clicks and everything else.

Evaluating chains: a stand-in

The stand-in below implements a tiny version of the Compose measuring rules with four modifiers: padding, size, background and fillMaxWidth. The content is a 60 by 20 "text". Each chain gets incoming constraints of at most 200 wide and 160 tall. The program prints the outer size and a picture where each character is 10 units: a letter for a background, # for the text.

Padding shrinks the constraints for what is inside and then adds its amount back to the reported size. size forces the constraints to an exact size (limited by what the parent allows). fillMaxWidth raises the minimum width to the maximum. A background paints the rectangle of whatever it wraps.

Kotlin Modifier order changes the result (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Compare the third and fourth chains. They contain the same pieces, in a different order:

  • size(100) then padding(20): the box is exactly 100 wide, and the padding eats into it. The content area is 60. The outer size is 100 by 100.
  • padding(20) then size(100): the padding is outside, and the size applies to what is inside it. The outer size is 140 by 140.

This is the practical difference between padding and size: padding adds space around something, while size fixes the dimensions of the node it is applied to, including everything inside the size modifier. A frequent real-world version: Modifier.size(48.dp).padding(12.dp) gives a 48 dp touch target with a 24 dp icon area, while the reverse gives a 72 dp target.

The modifiers you will use most

Size and fill: size(w, h), width, height, fillMaxWidth(), fillMaxHeight(), fillMaxSize() and fillMaxWidth(0.5f) for a fraction. wrapContentSize() lets content be its natural size inside a bigger area. Remember that all sizes obey the incoming constraints (layout lesson).

Spacing: padding(16.dp), padding(horizontal = 16.dp, vertical = 8.dp), padding(start = 8.dp), and offset(x, y), which moves drawing without changing the measured size.

Decoration: background(color, shape), border(width, color, shape), clip(shape) (cuts what is drawn to a shape, such as CircleShape), and shadow(...).

Interaction: clickable { }, toggleable, selectable, combinedClickable (click and long click), verticalScroll(rememberScrollState()), horizontalScroll and draggable. For accessible behavior, clickable accepts onClickLabel and role.

Accessibility: semantics { contentDescription = "..." } adds information for screen readers; Accessibility and Localization covers it.

Clickable and its area

clickable makes everything inside it (what comes after it in the chain) respond to taps, and it also draws the ripple on that area. So:

Kotlin
// read-only example: needs Android and Compose
Modifier.clickable { open() }.padding(16.dp)   // padding is part of the tap target
Modifier.padding(16.dp).clickable { open() }   // only the inner area is tappable

For small icons, make the padding part of the click area so that the touch target is large enough. Android recommends a minimum of 48 dp.

A hit-test stand-in and custom modifiers

The next stand-in models the click-area rule. A modifier is a value, so you can build it, store it and combine it. A custom modifier is just an extension function on Modifier; in Compose you write fun Modifier.card(): Modifier = this.background(...).padding(...), and the stand-in does the same with tappableRow. (Extension functions are covered in Extension functions.) The Avatar function shows the conventional modifier parameter: the caller's modifier is applied first, and the component adds its own after.

Kotlin Modifiers as values and the tap area (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

A tap in the 20-unit margin fires A but not B, because in a the click area wraps the padding. This is the same reason you should put clickable before padding when you want the margin tappable.

graphicsLayer and drawing modifiers

Modifier.graphicsLayer { ... } applies visual transformations to a composable's rendered content on a separate layer: alpha, scaleX, scaleY, rotationZ, translationX, translationY, shadowElevation and clip. It is the efficient way to animate such properties, because changes in the lambda affect drawing only and do not trigger new layout or composition:

Kotlin
// read-only example: needs Android and Compose
Box(
    Modifier
        .size(80.dp)
        .graphicsLayer {
            rotationZ = angle      // read inside the lambda: redraw only
            alpha = 0.8f
        }
        .background(Color.Blue),
)

For custom drawing, drawBehind { } and drawWithContent { } give you a canvas. They are advanced tools; the animation lesson uses graphicsLayer later.

Writing your own modifier

Most custom modifiers are plain extension functions that combine existing ones (like tappableRow above). If you need state or composable access inside a modifier, use Modifier.composed { } in older code or, preferably, the Modifier.Node APIs. Beginners should stay with extension functions. Two habits matter more than the machinery:

  • Accept modifier: Modifier = Modifier in every reusable composable and apply it first on the outermost element, so callers control the outside of your component.
  • Put your own modifiers after the caller's, so the caller's size and padding wrap your decorations rather than being hidden by them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which statement about a modifier chain is correct?

    Each modifier wraps everything after it in the chain, like the layers of an onion, so reordering changes the result.
  2. 2. What is the difference between Modifier.background(c).padding(16.dp) and Modifier.padding(16.dp).background(c)?

    A background paints the rectangle of whatever it wraps. In the first chain it wraps the padding, in the second it wraps only the content.
  3. 3. Modifier.padding(20.dp).size(100.dp) and Modifier.size(100.dp).padding(20.dp) are used on the same composable. What are their outer sizes?

    With padding first, the size applies inside it, so the total is 100 + 2 x 20 = 140 dp. With size first, the padding is inside the 100 dp box.
  4. 4. How do you make a small icon's padding part of its tap target?

    clickable applies to what comes after it in the chain. Putting clickable first makes the padding that follows part of the clickable area.
  5. 5. Why is graphicsLayer efficient for animating rotation or alpha?

    Values read in the graphicsLayer lambda only trigger a redraw of that layer, so animating them avoids composing and measuring again.

Summary

  • A Modifier is an immutable chain applied from the outside in; the order of modifiers changes the result.
  • padding adds space around what follows it; size fixes the dimensions of the node and everything inside it.
  • background, border and clip act on the area that the previous modifiers left; clickable makes what follows it tappable.
  • Put clickable before padding to make the padding part of the touch target, aiming for at least 48 dp.
  • graphicsLayer transforms drawing cheaply because reads inside it avoid recomposition and layout.
  • Custom modifiers are usually extension functions on Modifier; reusable composables take modifier: Modifier = Modifier and apply it first on their outermost element.
  • The Compose code is read-only; the TryIt blocks are stand-ins that model the rules.

Next: Text, Images and Icons