How can I get onTouchEvent in Jetpack Compose?

We have a separate package for that, which is pretty useful.
There are two main extension functions that would be suitable for you:

  • pointerInput – docs
  • pointerInteropFilter – docs

If you want to handle and process the event I recommend using pointerInteropFilter which is the analogue of View.onTouchEvent. It’s used along with modifier:

Column(modifier = Modifier.pointerInteropFilter {
    when (it.action) {
        MotionEvent.ACTION_DOWN -> {}
        MotionEvent.ACTION_MOVE -> {}
        MotionEvent.ACTION_UP -> {}
        else ->  false
    }
     true
})

That will be Compose adjusted code to your specified View.onTouchEvent sample.

P.S. Don’t forget about @ExperimentalPointerInput annotation.

Leave a Comment

tech