singletap touch detect in Ontouch method of the view
android, ontouchlistener, touch
Solution
I also ran into the same problem recently and ended up having to implement a debounce to get it working. It's not ideal, but it's pretty reliable until I can find something better.
View.onClickListener was much more reliable for me, but unfortunately I need the MotionEvent from the OnTouchListener.
Edit: Removed the excess code that would cause it to fail here
class CustomView extends View {
private static long mDeBounce = 0;
static OnTouchListener listenerMotionEvent = new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if ( Math.abs(mDeBounce - motionEvent.getEventTime()) < 250) {
//Ignore if it's been less then 250ms since
//the item was last clicked
return true;
}
int intCurrentY = Math.round(motionEvent.getY());
int intCurrentX = Math.round(motionEvent.getX());
int intStartY = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalY(0)) : intCurrentY;
int intStartX = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalX(0)) : intCurrentX;
if ( (motionEvent.getAction() == MotionEvent.ACTION_UP) && (Math.abs(intCurrentX - intStartX) < 3) && (Math.abs(intCurrentY - intStartY) < 3) ) {
if ( mDeBounce > motionEvent.getDownTime() ) {
//Still got occasional duplicates without this
return true;
}
//Handle the click
mDeBounce = motionEvent.getEventTime();
return true;
}
return false;
}
};
}
Problem
I needed a singletap touch detect in my custom view's ontouch method. I tried getting the x and y values in both ACTION-DOWN and ACTION-UP and in ACTION-UP I gave a condition that if a the values of X and Y in ACTIONDOWN and ACTION-UP are equal then take it as a single tap. My code is as follows ``` @Override public boolean onTouchEvent(MotionEvent ev) { if (!mSupportsZoom && !mSupportsPan) return false; mScaleDetector.onTouchEvent(ev); final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mLastTouchX = x; //here i get x and y values in action down mLastTouchY = y; mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); if (mSupportsPan && !mScaleDetector.isInProgress()) { final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; mPosX += dx; mPosY += dy; //mFocusX = mPosX; //mFocusY = mPosY; invalidate(); } mLastTouchX = x; mLastTouchY = y; break; } case MotionEvent.ACTION_UP: { final float x = ev.getX(); final float y = ev.getY(); touchupX=x; //here is get x and y values at action up touchupY=y; if(mLastTouchX == touchupX && mLastTouchY == touchupY){ //my condition if both the x and y values are same . PinchZoomPanActivity2.tapped1(this.getContext(), 100); //my method if the singletap is detected } else{ } mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = ev.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = ev.getX(newPointerIndex); mLastTouchY = ev.getY(newPointerIndex); mActivePointerId = ev.getPointerId(newPointerIndex); } break; } } return true; } ``` but I cant get it done. I mean at every action up my method is called. even when the x and y values of both actionup and actiondown are not same. and I think I also need to put some range to the singletap as we touch with our finger on the screen. Can anyone suggest me some ways?