Skip to content

Instantly share code, notes, and snippets.

@xzzz9097
Last active August 29, 2015 14:06
Show Gist options
  • Save xzzz9097/d433912691bc9b38cd5c to your computer and use it in GitHub Desktop.
Save xzzz9097/d433912691bc9b38cd5c to your computer and use it in GitHub Desktop.
Check if a particular touch event on a view is a click with onTouch instead of onClick.
/**
* Handy method that pairs with onTouchEvent to determine when the event is a click action.
* Works by checking difference from initial to final finger position and the duration of the
* event (to make sure event is not a long click).
* @param startX: initial position of the finger - for instance event.getX() in ACTION_DOWN
* @param endX: final position of the finger - for instance event.getX() in ACTION_UP
* @param duration: duration of the touch - has to be implemented
* @return whether the event is a click or not
*/
private boolean isAClick(float startX, float endX, long duration) {
final int MAX_DISTANCE = 5;
final int MAX_CLICK_DURATION = 100;
float differenceX = Math.abs(startX - endX);
return differenceX < MAX_DISTANCE && duration < MAX_CLICK_DURATION;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment