Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double click event in android

How to implement double click event in android without using gesturedetector?

like image 501
user386430 Avatar asked Jul 14 '10 11:07

user386430


People also ask

How do you handle double click?

We can perform double click on elements in Selenium with the help of Actions class. In order to perform the double click action we will use moveToElement() method, then use doubleClick() method. Finally use build(). perform() to execute all the steps.

How do I know if double click?

To detect double clicks with JavaScript you can use the event listener dblclick . The dblclick event is supported in all modern browsers for desktop/laptops, even Internet Explorer 11. Unfortunately, it's not supported on all mobile devices, yet.

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.


3 Answers

If you mean double tap you have to use GestureDetector.OnDoubleTapListener.

like image 141
Robby Pond Avatar answered Sep 23 '22 02:09

Robby Pond


I'm sure all the code there does is determine if the second click was within a certain time of the first click, otherwise treat it as a second click. That's how I would do it anyway.

like image 29
the_dirty_burger Avatar answered Sep 23 '22 02:09

the_dirty_burger


just use setOnTouchListener to record the first and second click time. If they are very close, determine it as a double click. Like this,

public class MyActivity extends Activity {

    private final String DEBUG_TAG= "MyActivity";
    private long firstClick;
    private long lastClick;
    private int count; // to count click times

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button mButton= (Button)findViewById(R.id.my_button);
        mButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // if the second happens too late, regard it as first click
                        if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
                            count = 0;
                        }
                        count++;
                        if (count == 1) {
                            firstClick = System.currentTimeMillis();
                        } else if (count == 2) {
                            lastClick = System.currentTimeMillis();
                            // if these two clicks is closer than 300 millis second 
                            if (lastClick - firstClick < 300) {
                                Log.d(DEBUG_TAG,"a double click happened");
                            }
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
    }
}
like image 25
wuliang8910 Avatar answered Sep 21 '22 02:09

wuliang8910