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.
+5
A:
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.
Romain Guy
2010-03-27 06:56:37
Thanks a ton for the info. The only problem I am running across is how to implement an OnTouchListener for multiple buttons. Am I to set an OnTouchListener for every one? Or can I set a single listener, and use a switch statement like switch(v.getId()) R.id.button1:..... R.id.button2:......
Pat
2010-03-27 07:32:59
nm, I figured it out :) Thanks again!
Pat
2010-03-27 07:43:44
By the way Romain - if you're 'the' Romain Guy from Google, could you try to stick to using one account on Stack Overflow? You appear to use up to 5 accounts, most of them new and with low reputation. That makes it hard for people to know whether the answers they're getting are 'official' and very reliable, or simply the opinion of someone who happens to share the same name as the guy (no pun intended) at Google.
Steve H
2010-03-27 12:09:26
Ok, so this is what I have so far. I have set an OnTouchListener for each of my buttons, and here is the code for the event: http://pastebin.com/10RyhKx0 The buttons operate just fine, I just cannot click more than 1 at a time
Pat
2010-03-27 17:27:26
Steve H., I am 'the' Romain Guy but StackOverflow was too annoying when I tried to login with existing accounts :)
Romain Guy
2010-03-28 06:23:36
Steve, I've created a real account, this should help :)
Romain Guy
2010-03-28 06:29:56
A:
here is a great tutorial that should solve your issue. http://blogs.zdnet.com/Burnette/?p=1747&tag=col1;post-1847
Dave.B
2010-03-27 18:20:53
Am I right when I say that you cannot click 2 'buttons' at the same time within the android API? I understand you can click the screen and get coords... however, do the button controls themselves allow 2 clicks at the same time. No matter how hard I try.. I cannot get it to work.
Pat
2010-03-28 19:15:12
+2
A:
@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;
}
Lo'oris
2010-05-03 12:02:15