I am doing a project in which i want to display a particular message on single touch and another message on double touch using android.How can i implement it.
My sample code is below
if(firstTap){
            thisTime = SystemClock.timeMillis();
            firstTap = false;
        }else{
            prevTime = thisTime;
            thisTime = SystemClock.uptimeMillis();
            //Check that thisTime is greater than prevTime
            //just incase system clock reset to zero
            if(thisTime > prevTime){
                //Check if times are within our max delay
                if((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY){
                    //We have detected a double tap!
                    Toast.makeText(AddLocation.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
                    //PUT YOUR LOGIC HERE!!!!
                }else{
                    //Otherwise Reset firstTap
                    firstTap = true;
                }
            }else{
                firstTap = true;
            }
        }
        return false;
                Pressing the default mouse button twice quickly opens or executes a program or opens a file. For example, while in Microsoft Windows or most other operating systems double-clicking a program icon opens that program.
The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example: // Make your activity class to implement View. OnClickListener public class MenuPricipalScreen extends Activity implements View.
We have extended the OnClickListener so it can be easily applied to any view:
/**
 * Allows to set double click events on views.
 *
 * @param action The action to perform on click.
 * @param interval The maximum interval between two clicks, in milliseconds.
 */
class DoubleClickListener(private val action: (view: View) -> Unit, private val interval: Long = 800) : View.OnClickListener {
    companion object {
        /** The view that was clicked previously. */
        private var myPreviouslyClickedView: WeakReference<View>? = null
        /**
         * Check if the click was a second one or not.
         * @param view The view to check for.
         *
         * @return True if the click was a second one.
         */
        private fun isSecondClick(view: View) =
            myPreviouslyClickedView?.get() == view
    }
    /** Execute the click. */
    override fun onClick(view: View?) {
        if (view != null) {
            // Make sure this click is the second one
            if (isSecondClick(view)) {
                myPreviouslyClickedView?.clear()
                action(view)
            } else {
                // Set the previous view to this one but remove it after few moments
                myPreviouslyClickedView = WeakReference(view)
                view.postDelayed({ myPreviouslyClickedView?.clear() }, interval)
            }
        }
    }
}
The property myPreviouslyClickedView ensures that there can be multiple views using the listener.
Part 2
Further, we made it a bit easier to assign the listener by extending the View:
/**
 * Set a double click listener on a view.
 *
 * @param action The action to perform on a double click.
 */
fun View.setOnDoubleClickListener(action: (view: View) -> Unit) =
    this.setOnClickListener(DoubleClickListener(action))
Or, to handle custom intervals between two clicks:
/**
 * Set a double click listener on a view.
 *
 * @param interval The maximum interval between two clicks, in milliseconds.
 * @param action The action to perform on a double click.
 */
fun View.setTimedOnDoubleClickListener(interval: Long, action: (view: View) -> Unit) =
    this.setOnClickListener(DoubleClickListener(action, interval))
                        Try to use GestureDetector.
public class MyView extends View {
GestureDetector gestureDetector;
public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // creating new gesture detector
    gestureDetector = new GestureDetector(context, new GestureListener());
}
// skipping measure calculation and drawing
// delegate the event to the gesture detector
@Override
public boolean onTouchEvent(MotionEvent e) {
    //Single Tap
    return gestureDetector.onTouchEvent(e);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();
        Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
        return true;
    }
}
                        here is full example double-tap listener in kotlin
class MainActivity : AppCompatActivity() {
    private lateinit var gDetector : GestureDetectorCompat
    private lateinit var touchListener : View.OnTouchListener
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // instantiate GestureDetectorCompat
        gDetector = GestureDetectorCompat(
                      this,
                      GestureDetector.SimpleOnGestureListener()
                    )
        // Create anonymous class extend OnTouchListener and SimpleOnGestureListener
        touchListener = object : View.OnTouchListener, GestureDetector.SimpleOnGestureListener() {
            override fun onTouch(view: View?, event: MotionEvent?): Boolean {
                gDetector.onTouchEvent(event)
                gDetector.setOnDoubleTapListener(this)
                return true
            }
            override fun onDoubleTap(e: MotionEvent?): Boolean {
                Log.i("debug", "onDoubleTap")
                return true
            }
        }
        anyView.setOnTouchListener(touchListener)
   }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With