I am currently using
onTouchEvent(MotionEvent event){
}
to detect when the user presses my glSurfaceView is there a way to detect when a long click is made. I'm guessing if I can't find much in the dev docs then it will be some sort of work around method. Something like registering ACTION_DOWN and seeing how long it is before ACTION_UP.
How do you detect long presses on android using opengl-es?
I have a code which detects a click, a long click and movement. It is fairly a combination of the answer given above and the changes i made from peeping into every documentation page.
I confirm it's working as I have used it in my own application.
The solution by MSquare works only if you hold a specific pixel, but that's an unreasonable expectation for an end user unless they use a mouse (which they don't, they use fingers).
So I added a bit of a threshold for the distance between the DOWN and the UP action in case there was a MOVE action inbetween.
I have created a snippet - inspired by the actual View source - that reliably detects long clicks/presses with a custom delay. But it's in Kotlin:
Try this:
The idea is creating a
Runnable
for execute long click in a future, but this execution can be canceled because of a click, or move.You also need to know, when long click was consumed, and when it is canceled because finger moved too much. We use
initialTouchX
&initialTouchY
for checking if the user exit a square area of 10 pixels, 5 each side.Here is my complete code for delegating Click & LongClick from
Cell
inListView
toActivity
withOnTouchListener
:ClickDelegate
is aninterface
for sending click events to the handler class like anActivity
And all what you need is to implement it in your
Activity
or parentView
if you need to delegate the behavior: