Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a long press with Android

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?

like image 445
Jack Avatar asked Oct 27 '11 17:10

Jack


People also ask

What is long press on Android?

More about the "touch & hold" gesture Many times, touch & hold lets you take action on something on your screen. For example, to move an app icon on your home screen, touch & hold, then drag it to the new location. Sometimes touch & hold is called a "long press."

What is used detect when the user maintains the touch over a view for an extended period?

onLongClickListener – Used to detect when the user maintains the touch over a view for an extended period. Corresponds to the onLongClick() callback method which is passed as an argument the view that received the event.

How long is a long press?

The default value is 500ms.


1 Answers

GestureDetector is the best solution.

Here is an interesting alternative. In onTouchEvent on every ACTION_DOWN schedule a Runnable to run in 1 second. On every ACTION_UP or ACTION_MOVE, cancel scheduled Runnable. If cancelation happens less than 1s from ACTION_DOWN event, Runnable won't run.

final Handler handler = new Handler();  Runnable mLongPressed = new Runnable() {      public void run() {          Log.i("", "Long press!");     }    };  @Override public boolean onTouchEvent(MotionEvent event, MapView mapView){     if(event.getAction() == MotionEvent.ACTION_DOWN)         handler.postDelayed(mLongPressed, ViewConfiguration.getLongPressTimeout());     if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))         handler.removeCallbacks(mLongPressed);     return super.onTouchEvent(event, mapView); } 
like image 138
MSquare Avatar answered Sep 20 '22 22:09

MSquare