K
K
Kruger2018-08-09 12:31:01
Android
Kruger, 2018-08-09 12:31:01

How to make LongPress inside onTouchEvent(Kotlin)?

There is a conditional code.

spoiler
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
Log.d(TAG,"TAP ")
}
MotionEvent.ACTION_MOVE -> {
}
MotionEvent.ACTION_UP -> {
}
MotionEvent.ACTION_CANCEL -> {
}
}
return true
}

One click can be handled like this (Although it's crooked), how can a long press be processed? The code is written inside the custom SurfaceView.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Chvarkov, 2018-08-09
@KrUgerD

The variant I use. In ACTION_DOWN, you need to remember the touch time, and in ACTION_UP, check how much time has passed. Conditionally <1500 ms - tap, more - long tap. If exactly after N time it is necessary to notify about a long tap, even if the user has not removed his finger, then I start the timer for this time. But then in ACTION_OUTSIDE / ACTION_POINTER_UP / ACTION_CANCEL you need to reset the timer.

D
Denis Zagaevsky, 2018-08-09
@zagayevskiy

You can use GestureDetector .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question