Single vs. Double Tap

This app plugs a View.OnTouchListener into the yellow TextView. The View.OnTouchListener passes each touch to the GestureDetector. When the GestureDetector feels it has received enough touches to constitute a gesture, the GestureDetector calls the appropriate method of the GestureDetector.SimpleOnGestureListener that was plugged into the GestureDetector.

Class GestureDetector.SimpleOnGestureListener is called “simple” because extending this class is simpler than implementing the interface GestureDetector.OnDoubleTapListener. Class GestureDetector.SimpleOnGestureListener already implements all of the methods of this interface with stubs that do nothing, so all we have to do is @Override the methods that we actually want to write. If we had implemented the interface, we would also have had to define a third method, onDoubleTapEvent, called for each tap of a double tap.

The gestureDetector is a local variable of the method onCreate of the activity class. It must be final to be mentioned in a method (onTouch) of another class (the View.OnTouchListener).

Source code in Tap.zip

  1. MainActivity.java
  2. activity_main.xml. The TextView has an android:id.
  3. strings.xml contains a string resource named instructions.
  4. AndroidManifest.xml
  5. build.gradle (Module: app).

Things to try

  1. How many milliseconds apart are the two taps of a double tap? Replace the onDoubleTap method of the GestureDetector.SimpleOnGestureListener with the following field and method. (I get about 80 milliseconds.)
                long beginTime; //time when double tap begins
    
                @Override
                public boolean onDoubleTapEvent(MotionEvent motionEvent) {
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            beginTime = motionEvent.getEventTime();
                            return true;
                        case MotionEvent.ACTION_UP:
                            long milliseconds = motionEvent.getEventTime() - beginTime;
                            Toast toast = Toast.makeText(MainActivity.this, milliseconds + " milliseconds", Toast.LENGTH_LONG);
                            toast.show();
                            return true;
                        default:
                            return false;
                    }
                }
    

    What is the maximum duration in milliseconds allowed between the two taps of a double tap? In GestureDetector.java, the DOUBLE_TAP_TIMEOUT in line 757 is defined in line 227, and ultimately in lines 480 and 97 of ViewConfiguration.java. Add the following to onCreate.

            Log.d("myTag", "ViewConfiguration.getDoubleTapTimeout() = " + ViewConfiguration.getDoubleTapTimeout());
    
    ViewConfiguration.getDoubleTapTimeout() = 300