I have an image (ImageView) on my screen. The image takes up roughly half of the screen which means that you can click both on the image and on the side of the image.
<
I ended up not setting any OnClickListener on the ImageView at all. Instead I only implement "public void onTouch(MotionEvent event)" and "public void onWindowFocusChanged(boolean hasFocus)" like this:
private int fieldImgXY[] = new int[2];
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Use onWindowFocusChanged to get the placement of
// the image because we have to wait until the image
// has actually been placed on the screen before we
// get the coordinates. That makes it impossible to
// do in onCreate, that would just give us (0, 0).
fieldImage.getLocationOnScreen(fieldImgXY);
Log.i(LOG_TAG, "fieldImage lockation on screen: " +
xyString(fieldImgXY[0], fieldImgXY[1]));
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.i(LOG_TAG, "touch event - down");
int eventX = (int) event.getX();
int eventY = (int) event.getY();
Log.i(LOG_TAG, "event (x, y) = " + xyString(eventX, eventY));
int xOnField = eventX - fieldImgXY[0];
int yOnField = eventY - fieldImgXY[1];
Log.i(LOG_TAG, "on field (x, y) = " + xyString(xOnField, yOnField));
}
return super.onTouchEvent(event);
}