Search code examples
androidandroid-jetpackandroid-jetpack-compose

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


This is what we would currently use in typical Views: https://developer.android.com/reference/android/view/View.OnTouchListener

Is there an equivalent in Jetpack Compose?


Solution

  • 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.