Skip to content Skip to sidebar Skip to footer

Multiple Button Presses For Android 2.x

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

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;
}

Post a Comment for "Multiple Button Presses For Android 2.x"