title: onSingleTapUp - only for tapping on Buttons
body: <p>I have two <code>Buttons</code> in my <code>Linear Layout</code>. I successfully override the <code>onTouch</code> of my <code>Buttons</code> so that when I fling over them something happens. I also want to be able to click on the <code>Buttons</code> so I override <code>onSingleTapUp</code>. </p>
  
  <p>The problem is that <code>onSingleTapUp</code> works not only on my <code>Buttons</code> but for everywhere else on my screen too. So is there a way to make <code>onSingleTapUp</code> only refer to tapping of <code>Buttons</code> instead of tapping anywhere on the screen?</p>
  
  <p>Attempt: I noticed <code>MotionEvent</code> has a boolean <code>isButtonPressed(int button)</code> so I tried the following but it crashed my application:</p>
  
  <pre><code>@Override
  public boolean onSingleTapUp(MotionEvent ev) {
      if (ev.isButtonPressed(1))
          Log.d("button", "override - click event performed");
      return true;
  }
  </code></pre>
  
  <p>Here is my full code:</p>
  
  <p>class MainActivity:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      protected Button bttn1, bttn2;
      private GestureDetector mGestureDetector;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mGestureDetector = new GestureDetector(this, new GestureListener());
  
          bttn1 = (Button) findViewById(R.id.bttn1);
          bttn2 = (Button) findViewById(R.id.bttn2);
  
          bttn1.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  Log.d("button1", "click event performed");
              }
          });
  
          bttn1.setOnTouchListener(new View.OnTouchListener() {
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                     if(mGestureDetector.onTouchEvent(event)) {
  
                          return true;
                      }
                      return false;
  
              }
          });
  
          bttn2.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  Log.d("button2", "click event performed");
              }
          });
  
          bttn2.setOnTouchListener(new View.OnTouchListener() {
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                     if(mGestureDetector.onTouchEvent(event)) {
  
                          return true;
                      }
                      return false;
  
              }
          });
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event)
      {
  
          boolean eventConsumed=mGestureDetector.onTouchEvent(event);
              if (eventConsumed)
              {
                  return true;
              }
              else
                  return false;
      }
  
  }
  </code></pre>
  
  <p>class GestureListener:</p>
  
  <pre><code>package com.example.swipetest;
  
  public class GestureListener extends GestureDetector.SimpleOnGestureListener{
  
      @Override
      public boolean onSingleTapUp(MotionEvent ev) {
          Log.d("button", "override - click event performed");
          return true;
      }
  
      @Override
      public boolean onDown(MotionEvent ev) {
        return true;
      }
  
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
          Log.d("fling", "fling performed");
          return true;
      }
  }
  </code></pre>