To get the touch position relative to the view add the touch event's (x,y) to your button's (x,y); to get the button's position relative to it's parent (your view) use the getLeft()
and getTop()
methods.
The documentation for View
has the following under Position:
It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.
Example code for your onTouch
method:
float screenX = v.getLeft() + event.getX(); // X in Screen Coordinates
float screenY = v.getTop() + event.getY(); // Y in Screen Coordinates