https://kotlinlang.org logo
#compose-android
Title
# compose-android
t

Tgo1014

03/16/2023, 2:47 PM
Thinking about touching and holding the finger on the screen. How can I detect the touch is over a specific composable?
j

jim

03/16/2023, 3:02 PM
It's not clear from your description what you're trying to accomplish. Are you trying to detect long press, or something more advanced? If you're trying to detect long-press, the following should work for you:
Copy code
Modifier.pointerInput(Unit) {
    detectTapGestures(
        onPress = { /* Called when the gesture starts */ },
        onDoubleTap = { /* Called on Double Tap */ },
        onLongPress = { /* Called on Long Press */ },
        onTap = { /* Called on Tap */ }
    )
}
For most advanced usecases, you can typically use a pointerInteropFilter to get input that is a little more raw and calculate your own gestures: https://stackoverflow.com/questions/64571945/how-can-i-get-ontouchevent-in-jetpack-compose You can also take a look at the implementation of
detectTapGestures
to get a better sense of how gesture detection is implemented.
t

Tgo1014

03/16/2023, 3:09 PM
It's not long press, I would need the user to tap a button and then some extra buttons would show, then the user can drag the finger to these buttons and when released it would trigger a callback
So it would be nice to have a way to detect the touch is over one of these input
j

jim

03/16/2023, 3:12 PM
pointerInteropFilter should do what you need.
t

Tgo1014

03/16/2023, 3:15 PM
Thanks for the north, I'll take a look on it 😄
13 Views