Search code examples
androidtouchproximitysensor

How to detect double tap on proximity sensor in android?


I am stuck at figuring out how to get the double tap even on proximity sensor. Since proximity sensor gives only true or false value how do I check if there is a continuation to form a double tap gesture ? I have found some of similar questions but no definite responses in terms of actual code. any example code would be very helpful!


Solution

  • Implement your own listener. When the sensor value changes from far to near, check when the last time it changed from far to near. If the difference in time is within your threshold for a double tap , then do your double tap action. Make sure to reset the time after a double tap is detected so that a triple tap does not trigger a second double tap event. You should use SystemClock.elapsedRealtime() to determine time between taps. You also can use the android default double tap timeout too, see ViewConfiguration.getDoubleTapTimeout()