What is the "View.onTouchListener" equivalent in Jetpack Compose? I need the touch coordinates

Viewed 2474
2 Answers

In compose beta-05 version, you can use:

Text("Your Composable", modifier = Modifier.pointerInput(Unit) {
    detectTransformGestures { centroid, pan, zoom, rotation ->
    }
    // or
    detectDragGestures { change, dragAmount ->  }
    // or
    detectTapGestures(
        onPress = { offset ->  },
        onDoubleTap = { offset -> },
        onLongPress = { offset -> },
        onTap = { offset ->  }
    )
    // or other similar...
})

With 1.0.0 you can use the PointerInput mod

For example you can use detectTapGestures:

Modifier.pointerInput(Unit) {
    detectTapGestures(
        onPress = {/* Called when the gesture starts */ },
        onDoubleTap = { /* Called on Double Tap */ },
        onLongPress = { /* Called on Long Press */ },
        onTap = { /* Called on Tap */ }
    )
}

or the detectDragGestures:

Box(
    Modifier
        .pointerInput(Unit) {
            detectDragGestures { change, dragAmount ->
                change.consumeAllChanges()
                //...
            }
        }
)

You can also use some modifiers like: .scrollable, .clickable, .draggable, .swipeable.

Related