views:

636

answers:

3

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
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
nm, I figured it out :) Thanks again!
Pat
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
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
Steve H., I am 'the' Romain Guy but StackOverflow was too annoying when I tried to login with existing accounts :)
Romain Guy
Steve, I've created a real account, this should help :)
Romain Guy
A: 

here is a great tutorial that should solve your issue. http://blogs.zdnet.com/Burnette/?p=1747&tag=col1;post-1847

Dave.B
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
You are wrong: you can do it of course, look at my code.
Lo'oris
+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