You can use a handler for this, but you need to forget to cancel the handler if the user moves his finger away from the screen. Yogesh is not entirely wrong, but the approach above simply adds a delay of 1000 ms between onClick and when runnable is executed. This means that if the user lifts his finger, running will work. This is not a real long press.
Below you can see that I am still using a handler with a delay of 1000 ms (you can set it the way you want), but delete the callback if the user lifts his finger or moves. If you want to get rid of the move trigger, just delete this part of the call. But in order to influence the long press, you need to consider the lift so that the user holds his finger all the time.
final Handler handler = new Handler(); Runnable mLongPressed = new Runnable() { public void run() { Log.i("", "Long press!"); } }; @Override public boolean onTouchEvent(MotionEvent event, View v){ if(event.getAction() == MotionEvent.ACTION_DOWN) handler.postDelayed(mLongPressed, 1000); if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP)) handler.removeCallbacks(mLongPressed); return false; }
Raarw source share