title: How to prevent onClick method on transparent portion of a PNG-loaded ImageView
body: <p>I am currently developing an Android app that displays multiple images (as <code>ImageView's</code>) stacked on top of each other.  Here is how the layers are currently configured:</p>
  
  <ul>
  <li>Background layer: scales the entire screen, must be clickable</li>
  <li>Foreground layer: scales the entire screen, must be clickable,
  contains transparency which allows the user to see some of the
  background layer</li>
  </ul>
  
  <p>The problem I face is with the foreground layer.  I am assigning the <code>onClick()</code> method to the imageview, but the method is being called whether they hit the portion of the image which is visible as well as the part which contains transparency.  I only want the foreground ImageView <code>onClick()</code> method to be called when the user clicks a portion of that imageview that is not transparent.</p>
  
  <p>This is what the scenario looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/sgpAV.png" alt="enter image description here"></p>
  
  <p>The diagonal lines represent the transparent portion of the Foreground image.  If a user touches this space, I want it to access the Background image instead of the Foreground image.  Thank you for any assistance you can provide.</p>
  
  <p><strong><em>Here is the solution I implemented (Thanks to answer below):</em></strong>  </p>
  
  <pre><code>//ontouchlistener - gets X and Y from event
  private void setClick(View view)
  {
      view.setOnTouchListener(new View.OnTouchListener() 
      {
          public boolean onTouch(View v, MotionEvent event) 
          {
              int imageId = getImageId((int)event.getX(), (int)event.getY());
              if (imageId &gt;= 0)
                  performActions(imageId);
              return false;
          }
      });
  }
  
  //get the ID of the first imageview (starting from foreground, 
  //working backwards) which contains a non-transparent pixel
  private int getImageId(int x, int y)
  {
      ViewGroup parent = (ViewGroup) findViewById(R.id.relative_layout);
      for (int a = parent.getChildCount()-1; a &gt;= 0; a--)
      {
          if (parent.getChildAt(a) instanceof ImageView)
              if (!checkPixelTransparent((ImageView)parent.getChildAt(a), x, y))
                  return parent.getChildAt(a).getId();
      }
      return -1;
  }
  
  //get bitmap from imageview, get pixel from x, y coord
  //check if pixel is transparent
  private boolean checkPixelTransparent(ImageView iv, int x, int y)
  {
      Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
      if (Color.alpha(bitmap.getPixel(x, y)) == 0)
          return true;
      else
          return false;
  }
  </code></pre>