Skip to content

Instantly share code, notes, and snippets.

@xaf-cv
Last active November 2, 2019 20:14
Show Gist options
  • Save xaf-cv/013c4e7a90a481e04f77 to your computer and use it in GitHub Desktop.
Save xaf-cv/013c4e7a90a481e04f77 to your computer and use it in GitHub Desktop.
Click&Hold detection through View.OnTouchListener()
View.OnTouchListener detectClickAndHoldListener = new View.OnTouchListener() {
private Timer timer = new Timer();
private long LONG_PRESS_TIMEOUT = 1337; // TODO: your timeout here
private boolean wasLong = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(getClass().getName(), "touch event: " + event.toString());
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// touch & hold started
timer.schedule(new TimerTask() {
@Override
public void run() {
wasLong = true;
// touch & hold was long
}
}, LONG_PRESS_TIMEOUT);
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// touch & hold stopped
timer.cancel();
if(!wasLong){
// touch & hold was short
}
timer = new Timer();
return true;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment