Reputation: 21
I am relatively new to this still, and I have been developing a small app that would benefit greatly from a user being able to press 2 buttons at one time. What is the best method for achieving this? I dont think that an OnClickListener works like that, and I have seen examples for doing this with an OnTouch event. However, I do not know how to set up button presses with an OnTouch event.
Upvotes: 2
Views: 11078
Reputation: 11863
@Override
public boolean onTouchEvent (MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_UP) {
// reset all buttons
...
}
else {
int count=event.getPointerCount(),vx1=-1,vy1=-1,vx2=-1,vy2=-1;
if (count>=1) {
vx1=(int)event.getX(0);
vy1=(int)event.getY(0);
}
if (count>=2) {
vx2=(int)event.getX(1);
vy2=(int)event.getY(1);
}
...
}
return true;
}
Upvotes: 3
Reputation: 98521
You would have to handle touch events yourself. With the multi-pointers (aka multi-touch) API it's very easy. Just override the onTouchEvent() method or register an OnTouchListener on your buttons.
Upvotes: 5