androidx.compose.foundation.interaction

InteractionSource

Interface · from org.jetbrains.compose.foundation:foundation:1.12.2

Every clickable component tracks what the user is doing with it: pressing it, hovering it with a mouse, focusing it with the keyboard, or dragging it. These events are interactions, and an InteractionSource is the stream they come from.

Components use them to draw their states: the ripple, the pressed shape of an Expressive button, the hover tint, the focus ring.

Most of the time you don't need it, and pass nothing: the component creates its own. You need it when your code must react to the state too, like changing a label while a button is held.

React to the pressed state of a buttonExpressive theme
React to the pressed state of a button
val interactionSource = remember { MutableInteractionSource() }
val pressed by interactionSource.collectIsPressedAsState()

// Only for this picture: press the button from code.
LaunchedEffect(Unit) { interactionSource.emit(PressInteraction.Press(Offset.Zero)) }

Button(
    onClick = {},
    interactionSource = interactionSource,
    shapes = ButtonDefaults.shapes(),
) {
    Text(if (pressed) "Release to send" else "Hold to send")
}

How to use it

  1. Create one and keep it: val interactionSource = remember { MutableInteractionSource() }.
  2. Pass it to the component: Button(..., interactionSource = interactionSource).
  3. Read the state you need, as a State<Boolean>:
val pressed by interactionSource.collectIsPressedAsState()
val hovered by interactionSource.collectIsHoveredAsState()
val focused by interactionSource.collectIsFocusedAsState()
val dragged by interactionSource.collectIsDraggedAsState()

The parameter takes a MutableInteractionSource: the component writes events into it, and you read them out.

Sharing one between elements

Pass the same source to two elements to make them look pressed together. For example, a whole card row pressed when its icon is pressed:

val source = remember { MutableInteractionSource() }
Row(Modifier.clickable(interactionSource = source, indication = ripple()) { open() }) {
    Icon(Icons.Filled.Star, null)
    Text("Favorites")
}

With Modifier.clickable, indication is what draws the pressed and hovered state; ripple() is the Material ripple, null draws nothing. See the Modifier guide.

All events

For full control, collect the raw events. Each press has a Press, then a Release or Cancel:

LaunchedEffect(interactionSource) {
    interactionSource.interactions.collect { interaction ->
        when (interaction) {
            is PressInteraction.Press -> println("down at ${interaction.pressPosition}")
            is PressInteraction.Release -> println("clicked")
            is PressInteraction.Cancel -> println("dragged away")
        }
    }
}

The other kinds are HoverInteraction, FocusInteraction and DragInteraction.

interface InteractionSource

Kinds of InteractionSource

Anywhere a InteractionSource is expected, you can pass any of these.

How to get one

Components create their own. To read it, pass your own remember { MutableInteractionSource() } to the interactionSource parameter.

Ready-made values and functions that return it

Or create one of the kinds listed above.

Used by

Parameters that take a InteractionSource.

Properties

interactions

val interactions: Flow<Interaction>

The events, as a Kotlin Flow. Collect it in a LaunchedEffect.