Göksel Güren
Göksel Güren

Reputation: 1479

Which is better approach for OnClick Implementation?

For implementation of onClick function which approach is better?

  1. Saving touch start / touch up coordinates and processing this values for closeness? Like, if starting point and up point close each other, let the click action start.
  2. Saving touch start / touch up time difference and processing this value? Like, if touch starting time and up time difference less than a value, let the click action start.

And why?

Upvotes: 0

Views: 130

Answers (2)

s.d
s.d

Reputation: 29436

Depends on how many kind of touch events you want to support:

  1. on Up : click

  2. on Up : without moving much - > click , moved -> swipe

  3. on Up : short duration - > click , long duration -> long press has been triggered, ignore.

  4. on Up and long press triggered : without moving much - > ignore , moved -> drag n drop

You go into details of duration and displacement , when you really need more kinds of touch events.Best approach depends on scenario. So, if your touchscreen doesn't have a notion of swipe or long press or drag n drop, you might just fire a click on every up event, simplest scenario.

Upvotes: 1

Budius
Budius

Reputation: 39846

If you absolutely must implement your own, I would use the option 2.

 if(motionEvent==MotionEvent.ACTION_UP){
    long duration = motionEvent.getDownTime() - .getEventTime();
    if(duration < THRESHOULD)
         click();
 }

Upvotes: 1

Related Questions