Differentiate Single click and double click of a imageView in android

android double click problem
how to avoid multiple button click at same time in android

I have tried the following code to differentiate single click and double click. Single click is ok. When I double click the imageview, code inside both the single click and double click part execute.

private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
    private long lastPressTime;
    boolean mHasDoubleClicked;

img_feat_orgn_item.setOnClickListener(new OnClickListener() {
 @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    findDoubleClick();
    }
    });

private boolean findDoubleClick() {
        // Get current time in nano seconds.
    long pressTime = System.currentTimeMillis();
        // If double click...
        if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
            mHasDoubleClicked = true;           
            startActivity(new Intent(
                    Wv_HomePage.this,
                    NewDonation.class));
            // double click event....
        } else { // If not double click....
            mHasDoubleClicked = false;
            Handler myHandler = new Handler() {
                public void handleMessage(Message m) {
                    boolean mHasDoubleClicked = false;
                    if (!mHasDoubleClicked) {
                        // single click event
                        feature_class_val = listData_Feature_Organization
                                .get(j);
                        Intent intent = new Intent(
                                Wv_HomePage.this,
                                OrganizationDetails.class);
                        Bundle b = new Bundle();
                        b.putString("orgn_name",
                                feature_class_val.name);
                        intent.putExtras(b);
                        startActivity(intent);

                    }
                }
            };
            Message m = new Message();
            myHandler.sendMessageDelayed(m, DOUBLE_PRESS_INTERVAL);
        }
        lastPressTime = pressTime;
        return mHasDoubleClicked;
    }

By convention, Android apps don't have double clicking.

Maybe you'd rather use onLongClick?

Although, if you really want double click check out GestureDetector.OnDoubleTapListener

Differentiate single and double tap on any view: Android, Global object outside the class private GestureDetector detector; // Place this inner class anywhere outside onCreate() public class GestureListener extends  Video shows you how to detect single and double click of a button in your Android app. Android doesn't provide double button click method by default, so this is one of the ways on how to implement it.

just a little change and my code works fine. I placed the mHasDoubleClicked boolean inside handler, that makes the trouble. The below code works.

img_feat_orgn_item.setOnClickListener(new OnClickListener() {
     @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        findDoubleClick();
    if (mHasDoubleClicked) {
                        Log.v("double click","double click");
    startActivity(new Intent(
                        Wv_HomePage.this,
                        NewDonation.class));
                    }
        }
        });

    private boolean findDoubleClick() {
            // Get current time in nano seconds.
        long pressTime = System.currentTimeMillis();
            // If double click...
            if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                mHasDoubleClicked = true;           

                // double click event....
            } else { // If not double click....
                mHasDoubleClicked = false;
                Handler myHandler = new Handler() {
                    public void handleMessage(Message m) {

                        if (!mHasDoubleClicked) {
                            // single click event
                            feature_class_val = listData_Feature_Organization
                                    .get(j);
                            Intent intent = new Intent(
                                    Wv_HomePage.this,
                                    OrganizationDetails.class);
                            Bundle b = new Bundle();
                            b.putString("orgn_name",
                                    feature_class_val.name);
                            intent.putExtras(b);
                            startActivity(intent);

                        }
                    }
                };
                Message m = new Message();
                myHandler.sendMessageDelayed(m, DOUBLE_PRESS_INTERVAL);
            }
            lastPressTime = pressTime;
            return mHasDoubleClicked;
        }

App Inventor for Android: Build Your Own Apps, case is the amount of time between each image being changed on your sprite. The difference is that it plays very short sounds, typically no longer than 5— 7 You use the Sound component for a single purpose: The Sound component Double-click the andyouch.mp3 file and then click OK in the File Uploader pop-​up  Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener.

Here's my code, which seem to work.

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MultiTapImageView extends ImageView{

    private GestureDetector gestureDetector;
    private MultiTapImageViewListener mListener;

    public interface MultiTapImageViewListener {

        void onDoubleTap();
        void onSingleTap();

    }

    public MultiTapImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());

    }

    public void setDoubleTapListener(MultiTapImageViewListener listener){
        mListener = listener;
    }


    @Override
    public boolean onTouchEvent(MotionEvent e) {
        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();

            if(mListener != null){
                mListener.onDoubleTap();
            }

            return true;
        }


        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if(mListener != null){
                mListener.onSingleTap();
            }

            return true;
        }
    }
}

How to properly differentiate single-clicks and double-click in , Coroutines are fun: using UnityEngine; using System.Collections; public class DoubleClickTest : MonoBehaviour { private float doubleClickTimeLimit = 0.25f;  Simple double-click listener that can be used like the way OnClickListener is used on a view in Android - DoubleClickListener.java Skip to content All gists Back to GitHub

Try this.

   btn.setOnClickListener(new View.OnClickListener() {

        volatile int i = 0;

        @Override
        public void onClick(View v) {

            i++;
            Handler handler = new Handler();

            Runnable r = new Runnable() {
                @Override
                public void run() {
                    if (i == 1) {
                        //single click logic
                    }
                }
            };

            if (i == 1) {
                handler.postDelayed(r, 150);
            } else if (i == 2) {
                handler.removeCallbacks(r);
                i = 0;
                //Double click logic
            }
        }
    }
    );

Or you can check DOUBLE-TAP example from following URL. that is used in listView. i hope it is useful for you.

https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master

What is the expected timeframe of a double click?, Instead of double clicking to finish, you could: Click the starting point to close the shape (assuming all shapes are closed in); Have a button nearby labeled  Create an image view, given a drawable. you can set the max size of this imageview as well. 16. download images from the Internet and binds those with the provided ImageView.

btn.setOnClickListener(new View.OnClickListener() {

                               volatile int i = 0;

                               @Override
                               public void onClick(View v) {

                                   i++;
                                   Handler handler = new Handler();

                                   Runnable r = new Runnable() {
                                       @Override
                                       public void run() {
                                           if (i == 1) {
                                               //single click logic

                                               i = 0; //set to zero 
                                           }
                                       }
                                   };

                                   if (i == 1) {
                                       handler.postDelayed(r, 300);
                                   } else if (i == 2) {
                                       handler.removeCallbacks(r);
                                       i = 0;
                                       //Double click logic

                                   }
                               }
                           }
    );

Android Development Patterns: Best Practices for Professional , ACCESS_FINE_LOCATION"/> To demonstrate the difference in how these locations are reported, a simple application can be built that gets the device location Listing 11.1 Contents of MainActivity.java Click here to view code image package GPS_PROVIDER); if (fineLocation != null) { double latitude = fineLocation. Is it possible to do double-click on Android like you do on a regular computer? Some websites require double-click to operate, and I was hoping I could do the same on Android and get a computer double-click behavior instead of Android double tap zoom.

GestureDetector.OnDoubleTapListener, The listener that is used to notify when a double-tap or a confirmed single-tap occur. Summary. Public methods. abstract boolean, onDoubleTap(  See the code snippet below and ask if you have any doubt. Hope this helps.All the Best.

Hands-On Mobile App Testing: A Guide for Mobile Testers and Anyone , Tests can be executed on only a single device at a time, and Robotium is able to setGPSMockLocation(double latitude, double longitude, double altitude): sets the Spoon Spoon8 is another powerful Android test automation framework, developed by Listing 5.2 Sample Code from Spoon Click here to view code image  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). Ok, you have to do is: 1. Take one global variable to…

Detect common gestures, Capture touch events for an Activity or View; Capture touch events for a single However, if your app uses common gestures such as double tap, long press,  @Patil2421 said: @Moto: There is nothing like double click event with button, you just have a public event for Clicked and that is for single click, but you can detect this if you are using tapGesture in this you can detect the no. of taps and perform different actions accordingly.

Comments
  • in your code where I can put the single as well as double click code?
  • I used your code, when I double click, the code inside the single click also execute
  • Check again my answer.
  • @Manikandan you are right. Sorry for this. I edit my answer and check it on phone. Check it now. Now it works.