My parent view is a RelativeLayout, I added a triangle to it extending the View class. I draw the triangle using canvas and paint. The problem i am facing is that, when i touch on the relative layout, both touch listeners , the relative layout and triangle are being triggered. I just want the triangle to take the exact space as it requires. How can i limit my customview , triangle in this case to take it’s space, rather than occupying the entire parent layout.
Actually my requirement is: i have one relative layout on the layout i am adding some custom views dynamically. those have touch listeners for for dragging points in the triangle,but those are taking full screen of my parent view, because of that i am unable to trigger touch listeners separately for parent and child view’s.
I’ve spent countless hours developing solutions for this problem and I just couldn’t get my head around it. Any help would be greatly appreciated.
From what I am gathering you are saying,you have a triangle drawn within a
RelativeLayoutin aCustomView. The actualViewsize isFILL_PARENTfor both width and height. So the square representing the touchable area of the triangle is the same as the square representing the touchable area of the parentRelativeLayout.You would like touch events inside the triangle’s drawn area to be received by the triangle
Viewand anything outside to be received by the parent. It is not possible to define a custom touch area from what I understand.That doesn’t stop you manipulating drawing bounds and touch focus to accomplish what you want though.
How it can be done
Ensure that your
CustomViewwith the triange always receives the touch event. When it receives the touch event, it perform basic maths to figure out if the touch was within your triangle area (you must have your triangle bounds you use to draw your triangle saved as global variables for theCustomView).Then a simple conditional statement to determine whether to act on and consume or pass the event on (do you even want a
TouchEventon yourRelativeLayoutto do anything? If not that makes this a lot easier).Example code skeleton:
}
To be honest, the question is way to open-ended / unclear to give anything more specific. But this is how you would accomplish what you asked. Just apply it to your specific scenario.
Note: I have encountered issues with
TouchEventpassing, so if you really do want to pass them to specific places, you may have to investigate toying with View.dispatchTouchEvent() if the order your Touches are being processed becomes an issue.