Skip to content

Instantly share code, notes, and snippets.

@jokamjohn
Forked from xaf-cv/snippet.java
Created January 12, 2017 14:40
Show Gist options
  • Save jokamjohn/36eed047a0b399e6d75067b3400869db to your computer and use it in GitHub Desktop.
Save jokamjohn/36eed047a0b399e6d75067b3400869db 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