I was wondering if there is a way to know exactly where a button was tapped, and take different actions based on where the user tapped it. Something like:
fooBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(onClickLocation){
case LEFT:
foo();
break;
case RIGHT:
bar();
break;
case MIDDLE:
baz();
break;
}
}
});
Not using an
OnClickListener
.OnTouchListener
gives you aMotionEvent
that you can use to determine where the actual touch event occurred.For example, here I register both an
OnClickListener
and anOnTouchListener
on the sameView
(calledrow
):In this case, I don't need to know where the widget was touched for processing the click, but I do need to know where the widget was touched for adjusting the
RippleDrawable
background, so the ripple appears to emanate from where the user touched. Returningfalse
fromonTouch()
means I am not consuming the touch event, and so eventually myonClick()
method will also be called.In your case, either:
do the actual work in
onTouch()
, orcache the last-seen touch point in
onTouch()
but do not do the work untilonClick()
My gut tells me that the latter should give you better results (e.g., you won't misinterpret a long-click), but I have not tried doing what you are seeking.