Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement double click in android [duplicate]

Tags:

android

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;
like image 644
Sanal Varghese Avatar asked Apr 02 '12 05:04

Sanal Varghese


People also ask

Which activity is done on Start button click or double click?

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.

How do you avoid multiple buttons clicking at the same time in flutter?

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.


3 Answers

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))

like image 189
Sabo Avatar answered Oct 28 '22 11:10

Sabo


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;
    }

}
like image 31
Bhavin Avatar answered Oct 28 '22 09:10

Bhavin


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)

   }
}
like image 34
Muhammad Rio Permana Avatar answered Oct 28 '22 11:10

Muhammad Rio Permana