0
votes

I'm new to Android programming and I've coded a simple game where two players have to press their own button to reach the goal. What's the problem?

  • I've tested the app on Galaxy Nexus 4.0.3 and works perfectly.
  • Just tested on HTC Desire 2.3.7 and doesn't work as expected.

But while on Galaxy Nexus the 2 player can press the buttons at the same time, on the HTC Desire they cannot.

Both devices have multitouch capability.

Thanks

3
Note that the Desire has somewhat limited multitouch due to the used sensor. There are some samples on YouTube. Basically if the buttons are on the same X or Y coordinate, it might mix things up. You can work around that by placing the buttons into top-left and bottom-right or similar positions. - user658042
So this is not a bug of Android 4.0 as Cehm said. The only thing I can do is to alert users that 2vs2 mode works only on multitouch (real multitouch) devices... Am I right? - Angelo Tricarico
Either this, or trying to work around as I described. Maybe provide an option that's defaulted to failsafe. I had an SNES emulator when the Desire was new, which had that option. Seems to be pulled from the market though. If you are in the mood you can look how others solved it. Try out a few of these apps on the market, you might find something similar (since almost all NES emulators should suffer from similar problems in theory) - but yes, afterall that's exactly the reason why android caught up so late on true multitouch stuff. - user658042
How can I thank you now? :) I can't select your answer... - Angelo Tricarico
Well, Luminger said almost the same thing, so give him credit instead. :) - user658042

3 Answers

0
votes

Instead of using TWO different buttons you should have a layout over your views and define, within this views 'zones' where both players can press...

In fact, what you should do is using the multitouch capability of your android device (like pinch to zoom etc..) but this time it'll be used with 2 different hands.

The fact that you can press 2 different buttons on Android 4.0 'should' be a bug, TWO button shall not be able to be pressed at the same time because each button have an onClickListener running within the same thread...

0
votes

As far as I know the HTC Desire (and also it's sister Nexus One) aren't really multitouch capable. It's more a fake, I think they tell you a "square" which is pressed (unsure).

There are programs on the market which are able to visualize what the device recognized, you should install one of them on both devices and see where the differences are when you touch them in the same, multitouch, way.

There are also more resources on the net about it, it's well known.

0
votes
package whaever.package.used;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

// Modify this code as you wish.
// See if it works for you.
// I was looking for the same thing (press buttons and assync operation) on
// stackoverflow.com ...
// That is how I got to your question ... VERY LATE indeed (sorry, I am not
// working very much with Android, sorry for the HUDGE delay)
// Yet after reading the answers and after solving it for my particular case,
// I decided to provide this answer, maybe it is usefull and will help someone.
// Please be carefull as in my particular case I have much more code going
// with it. !!! This is only a model !!!

public class MainActivity extends AppCompatActivity {

    private final long butId_00_Down_Moment[] = new long[1];
    private final long butId_00_Up_Moment[] = new long[1];
    private final long butId_00_Duration[] = new long[1];

    private final long butId_01_Down_Moment[] = new long[1];
    private final long butId_01_Up_Moment[] = new long[1];
    private final long butId_01_Duration[] = new long[1];

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button butId_00 = (Button) findViewById(R.id.but_id00);
        butId_00.setOnTouchListener (new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        butId_00_Down_Moment[0] = System.currentTimeMillis();
                        butId_00_Duration[0] = 0;
                        MainActivity.this.findViewById(R.id.but_id00).performClick();
                        butId00_OnDown(view);
                        return true;
                    }
                    case MotionEvent.ACTION_UP: {
                        butId_00_Up_Moment[0] = System.currentTimeMillis();
                        butId_00_Duration[0] = butId_00_Up_Moment[0] - butId_00_Down_Moment[0];
                        MainActivity.this.findViewById(R.id.but_id00).performClick();
                        butId00_OnUp(view);
                        return true;
                    }
                }
                return false;
            }
        });
        butId_00.setOnClickListener(new View.OnClickListener () {
            @Override /**/ public void onClick (View view) { butId00_OnClick(view); }
        });

        final Button butId_01 = (Button) findViewById(R.id.but_id01);
        butId_01.setOnTouchListener (new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        butId_01_Down_Moment[0] = System.currentTimeMillis();
                        butId_01_Duration[0] = 0;
                        MainActivity.this.findViewById(R.id.but_id01).performClick();
                        butId01_OnDown(view);
                        return true;
                    }
                    case MotionEvent.ACTION_UP: {
                        butId_01_Up_Moment[0] = System.currentTimeMillis();
                        butId_01_Duration[0] = butId_01_Up_Moment[0] - butId_01_Down_Moment[0];
                        MainActivity.this.findViewById(R.id.but_id01).performClick();
                        butId01_OnUp(view);
                        return true;
                    }
                }
                return false;
            }
        });
        butId_01.setOnClickListener(new View.OnClickListener () {
            @Override /**/ public void onClick (View view) { butId01_OnClick(view); }
        });
    }

    // For the first button "butId_00"
    private void butId00_OnDown(View view) {
        // TODO your code here
        Logger.getGlobal().info("butId00_OnDown" + butId_00_Down_Moment[0]);
    }
    private void butId00_OnClick(View view) {
        // TODO your code here
        Logger.getGlobal().info("butId00_OnClick" + butId_00_Duration[0]);
    }
    private void butId00_OnUp(View view) {
        // TODO your code here
        Logger.getGlobal().info("butId00_OnUp" + butId_00_Up_Moment[0]);
    }

    // For the second button "butId_01"
    private void butId01_OnDown(View view) {
        // TODO your code here
        Logger.getGlobal().info("butId01_OnDown" + butId_01_Down_Moment[0]);
    }
    private void butId01_OnClick(View view) {
        // TODO your code here
        Logger.getGlobal().info("butId01_OnClick" + butId_01_Duration[0]);
    }
    private void butId01_OnUp(View view) {
        // TODO your code here
        Logger.getGlobal().info("butId01_OnUp" + butId_01_Up_Moment[0]);
    }
}