I've seen this with other platforms (especially iPhone/iPad) but have not been able to find-it/figure-it-out for Android and the other platforms don't seem to get much farther then asking the question.
I have a game that has a multiplayer component where two players have to press buttons on the same screen at the same time. Everything but simultaneous button presses is done.
As of right now it seems like Android calls the button's onTouchEvent and, since it is handled, it doesn't call the other button. Do you think something like having it call both buttons (if a touch is within the area of the button) would work? Is there any other way this could possibly be implemented?
Thanks for your time.
Anwser:
I ended up implementing two distinct touch points in the Activity's onTouchEvent function.
The basic implementation:
public class MainClass extends Activity {
// ...
btn1 = (CustomButton)findViewById(R.id.button_one);
btn2 = (CustomButton)findViewById(R.id.button_one);
btn1.ignoreMotionEvent(true);
btn2.ignoreMotionEvent(true);
// ...
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(r1 == null)
{
//r1 and r2 are Rect that define the absolute region of the button.
//They are both defined here to ensure that the everything will be layed out (didn't always work so just for extra "sure-ness" did it here)
}
CustomButton btn = null;
MotionEvent mevent = null;
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
int x = (int)event.getX();
int y = (int)event.getY();
if(r1.contains(x, y))
{
btn = btn1;
}
else if(r2.contains(x, y))
{
btn = btn2;
}
if(btn != null)
{
mevent = event;
}
break;
}
if(btn != null)
{
btn.ignoreMotionEvent(false);
btn.onTouchEvent(mevent);
btn.ignoreMotionEvent(true);
}
return super.onTouchEvent(event);
}
}
CustomButton is simply an extended version of Button where a method ignoreMotionEvent exists, this simply sets a boolean determining if CustomButton's onTouchEvent function should return false instantly or call super.onTouchEvent.
Unfortunately if you tried this code it wouldn't work. So why mention it? It is the basic outline of the code. The issue I encountered was the functions to make this work don't exist on Android 1.6 and earlier. Eclair and higher is required to make this work.
Now just to help out any other who found this because they had the same issue, here nearly an exact copy of what I am running:
public class MainClass extends Activity {
// ...
btn1 = (CustomButton)findViewById(R.id.button_one);
btn2 = (CustomButton)findViewById(R.id.button_one);
btn1.ignoreMotionEvent(true);
btn2.ignoreMotionEvent(true);
// ...
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(r1 == null)
{
//r1 and r2 are Rect that define the absolute region of the button.
//They are both defined here to ensure that the everything will be layed out (didn't always work so just for extra "sure-ness" did it here)
}
if(Utils.isEclairOrLater())
{
//Eclair and later
boolean froyo = Utils.isFroyoOrLater();
int action = froyo ? event.getActionMasked() : event.getAction();
CustomButton btn = null;
MotionEvent mevent = null;
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
int x = (int)event.getX();
int y = (int)event.getY();
if(r1.contains(x, y))
{
btn = btn1;
}
else if(r2.contains(x, y))
{
btn = btn2;
}
if(btn != null)
{
mevent = event;
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
int index = froyo ? event.getActionIndex() : ((action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT);
x = (int)event.getX(index);
y = (int)event.getY(index);
if(r1.contains(x, y))
{
btn = btn1;
}
else if(r2.contains(x, y))
{
btn = btn2;
}
if(btn != null)
{
mevent = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), (action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN ? MotionEvent.ACTION_DOWN : MotionEvent.ACTION_UP, event.getX(index), event.getY(index), 0);
}
break;
}
if(btn != null)
{
btn.ignoreMotionEvent(false);
btn.onTouchEvent(mevent);
btn.ignoreMotionEvent(true);
}
}
else
{
//Earlier then Eclair
CustomButton btn = null;
MotionEvent mevent = null;
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
int x = (int)event.getX();
int y = (int)event.getY();
if(r1.contains(x, y))
{
btn = btn1;
}
else if(r2.contains(x, y))
{
btn = btn2;
}
if(btn != null)
{
mevent = event;
}
break;
}
if(btn != null)
{
btn.ignoreMotionEvent(false);
btn.onTouchEvent(mevent);
btn.ignoreMotionEvent(true);
}
}
return super.onTouchEvent(event);
}
}
Utils is a simple utility class that, for the purpose of this example, gets the Build.VERSION.SDK_INT value and compares it to Eclair (5) and Froyo (8) for the appropriate functions.
Also it requires you compile with 2.2 or higher. If you don't know, Android is backwards-compatable as long as you do the checks for missing functions due to version differences. This means though you are compiling with 2.2, it will work all the way back to 1.0.
That is what I use and hopefully it helps others.