-
-
Save omidraha/368f4dd003775ec50378bde4f39c3586 to your computer and use it in GitHub Desktop.
Click&Hold detection through View.OnTouchListener()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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