Function

awaitLongPressOrCancellation

Waits for a long press by examining pointerId.

AwaitLongPressOrCancellationSample

/** Simple [awaitLongPressOrCancellation] demo. */
@Composable
@Sampled
fun AwaitLongPressOrCancellationSample() {
    var count by remember { mutableStateOf(0) }

    Column {
        Text("Long Press to increase count. Long Press count: $count")
        Box(
            Modifier.fillMaxSize()
                .wrapContentSize(Alignment.Center)
                .size(192.dp)
                .pointerInput(Unit) {
                    awaitEachGesture {
                        val down = awaitFirstDown(requireUnconsumed = false)
                        awaitLongPressOrCancellation(down.id)?.let { count++ }
                    }
                }
                .clipToBounds()
                .background(Color.Blue)
                .border(BorderStroke(2.dp, Color.Black))
        )
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: