2

Here is the scenario; I have 3 PNG photos i wanna use as background of buttons or ImageViews and they are overlapping in a relative layout. kinda like this:

RelativeLayout

so the red button will be the biggest and go under all of them, then i will add green button on top of red and then yellow button on top of green. so that's how it looks like. each button has a PNG background as i said at the beginning.

Problem is I cant make the only visible area of each Button/ImageView clickable! Android kinda considers each at rectangle button/ImageView. Any solution for this?

kevoroid
  • 5,052
  • 5
  • 34
  • 43
  • You have to implement an OnTouchListener which checks whether or not the user touched unto the image itself or not. – LuigiPower Sep 16 '13 at 12:18
  • i did; didnt work, still half of the green for example is considered as yellow. – kevoroid Sep 16 '13 at 12:28
  • You have to use the touch coordinates given to you by the MotionEvent to check if they are inside the image. It's not an easy calculation in that case. – LuigiPower Sep 16 '13 at 12:42
  • 1
    i found this - http://stackoverflow.com/questions/13861416/android-custom-shape-button - but i dont know what to replace bitmap.getPixel with since i used ImageView or button. any suggestion? – kevoroid Sep 16 '13 at 12:51
  • You can always get another instance of the image you're using as a Bitmap, then use that to call "getPixel" – LuigiPower Sep 16 '13 at 12:55
  • 1
    [Custom Shape Button which ignores touches on transparent background.](http://stackoverflow.com/a/19583072/2678584) – aniki.kvn Oct 25 '13 at 07:15

1 Answers1

1

In your onTouchListener you should check whether the event (MotionEvent) is in the transparent area of the background or not.

Either you can make a separate onTouchListener for each view/button and return false if the event is in the transparent area (of the View argument) or you can make a single listener for all of the buttons, ignore the View argument and check all of your three views to determine in which one the event is.

stan0
  • 11,549
  • 6
  • 42
  • 59