Search code examples
javaandroidbuttonontouchlisteneronlongclicklistener

How to check to see if two buttons are HELD together at same time ANDROID?


I have two circular buttons, each a different color.

Now if both buttons are held at the same time, I want them to both turn a completely new 3rd color, along with a sound effect, some graphical changes, etc.

The following code seems to work about 50% of the time, usually only when both buttons are pressed perfectly together. (If one button is held, and then the other is held shortly after, even by a hair, the app seems to crash).

    new Thread(new Runnable() {
        public void run(){
            while (keepgoing) {
                if (held1== true && held2==true) {
                    player2.start();
                    greenbutton.setBackground(getResources().getDrawable(R.drawable.brickbutton));
                    pinkbutton.setBackground(getResources().getDrawable(R.drawable.brickbutton));
                    keepgoing = false;
                    return;
                }
            }
            return;
        }

   }).start();


}

held1 is set to true when the button1.setOnLongClick event happens. held1 is set to false when the MotionEvent.ACTION_UP case happens for an OnTouchListener for button1.

same for button 2, like so.

      greenbutton.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
              held1 = true;
              player.start();
              if (switched){
                  revertButtons();
                  return true;
              }
              else {
                  switchButtons();
                  return true;
              }
          }
      });

      greenbutton.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction()){
                  case MotionEvent.ACTION_UP:
                      held1 = false;
                      player.start();
                      if (bricked){
                          return true;
                      }
                      if (switched){
                          revertButtons();
                          break;
                      }
                      else {
                          switchButtons();
                          break;
                      }
              }
              return false;
          }
      });

why is this code so unstable?


Solution

  • How about doing something like this,

    while(held1 == true){
    if(held2 == false){
    //do something
    }else{
    player2.start();
                        greenbutton.setBackground(getResources().getDrawable(R.drawable.brickbutton));
                        pinkbutton.setBackground(getResources().getDrawable(R.drawable.brickbutton));
                        keepgoing = false;
                        return;
    }
    }
    

    The idea is to imitate observer pattern like solution. Not only when they are both held together like you did, but to observe the second one so that at any point if they are both clicked then your work will be done. This is the beauty of RxJava/Rxandroid observer pattern. It would be perfect solution in your case.