-2

Is there any way to check, if two buttons are clicked at the same time ? I wrote a switch-statement in the button click listeners, but I can't check when these buttons are clicked ate the same time.

Here is a my code:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
            Log.d("MR.bool", "Button1 was clicked ");
            break;
        case R.id.button2:
            Log.d("MR.bool", "Button2 was clicked ");
            break;
        default:
            break;
    }
}
Aiko West
  • 791
  • 1
  • 10
  • 30
BekaKK
  • 2,173
  • 6
  • 42
  • 80

1 Answers1

0

I think there are always milliseconds between the clicks of the buttons. So they will never be clicked at the same time.

You can go with setOnTouchListener and while Button1 is pressed you can wait for Button2 is pressed

 button.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){

                    // wait here for Button2.Click

                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    // Button Release Event

                }
                return true;
            }

        });

You need call this with a Runnable on a Handler as mentioned in this questions answer: android repeat action on pressing and holding a button

Aiko West
  • 791
  • 1
  • 10
  • 30
  • Thanks for your response,but I did not understand correctly milliseconds logic @K. Dexter – BekaKK May 21 '19 at 08:45
  • It wont happen that you click 2 buttons at the exact same moment. So the only way to recognize some similar behavior is to check, if while button 1 is pressed, another button is pressed – Aiko West May 21 '19 at 09:33
  • @K.Dexter its logical. – RKRK May 23 '19 at 08:39