I\'ve got a custom view and I want to get the X and Y coordinates of a user click. I\'ve found that I can only get the coordinates from the onTouchEvent and an onClickEvent
You should use GestureDetector. Subclass GestureDetector.SimpleOnGestureListener and override onDown(MotionEvent event) method to return true. Then override onSingleTapConfirmed. Pass the listener to the GestureDetector constructor. Then call setOnDoubleTapListener with that listener as a parameter:
// in your view class constructor
GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// do whatever you want
return true;
}
};
mDetector = new GestureDetector(getContext(), listener);
mDetector.setOnDoubleTapListener(listener);
Then, you can pass MotionEvents to the detector:
@Override
public boolean onTouchEvent(MotionEvent event) {
return mDetector.onTouchEvent(event);
}