title: Single method to implement onTouchListener() for multiple buttons
body: <p>I am trying to see if there is a way to create a single method to implement a touch listener for multiple buttons, seeing as I have quite a few buttons that do almost exactly the same thing. The only difference in what they do is the message that they would send through my sendMessage() method, and how long the button needs to be held down for the message to be sent. If there is a way to do it, what might that be? And also, why wouldn't something like this work?</p>
  
  <pre><code>//Within onCreate Method...
  Button mButton = (Button) findViewbyId(R.id.three_sec_button);
  mButton = addTouchTimer(mButton, 3, 3);
  </code></pre>
  
  <p>Calls -</p>
  
  <pre><code>private Button addTouchTimer(Button button, final int sec, final int messageNum){
          button.setOnTouchListener(new View.OnTouchListener() {
              boolean longEnough = false;
              long realTimeLeft = sec * 1000;
              @Override
              // This will make it so that a message is only sent if the button is held down for 3 seconds
              // Otherwise it won't send. It is sent during the hold down process, releasing it returns a false
              // value and no message is sent.
              public boolean onTouch(View arg0, MotionEvent arg1) {
                  Log.d("Button", "Touchy Touchy!");
                  if(arg1.getAction() == MotionEvent.ACTION_DOWN){
                      buttonPressTime = new CountDownTimer(realTimeLeft, 1000){
                          @Override
                          public void onTick(long millisUntilDone){
                                  realTimeLeft = millisUntilDone;
                          }
  
                          @Override
                          public void onFinish() {  
                              long timeLeft = realTimeLeft;
                              long currTime = System.currentTimeMillis();
                              long realFinishTime = currTime + timeLeft;
                                  while(currTime &lt; realFinishTime){
                                      currTime = System.currentTimeMillis();
                                  }
                              longEnough = true;
                              sendEmergencyMessage(longEnough, messageNum);
                          }
                      }.start();
                  }
                  else if(arg1.getAction() == MotionEvent.ACTION_UP){
                      buttonPressTime.cancel();
                      sendMessage(longEnough, messageNum);
                  }
                  return longEnough;
              }           
          });
  
          return button;
      }
  </code></pre>
  
  <p>It just seems that for efficiency's sake there has to be a better way of doing it than implementing individual listeners for each button. As a note, sendMessage() has a Log call in it that utilizes the boolean value, I want to see what it is set as when it is passed. That is the only reason it is called during the release of the button.</p>