Multiple button presses for Android 2.x

10,608

Solution 1

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.

Solution 2

@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;
}
Share:
10,608
Admin
Author by

Admin

Updated on October 24, 2022

Comments

  • Admin
    Admin over 1 year

    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.