Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement double click for button in Android

How can I implement double click for a button in Android? Should I use OnDoubleTapListener?

like image 528
Anju Avatar asked Jan 31 '11 09:01

Anju


2 Answers

int i = 0;
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        i++;
        Handler handler = new Handler();
        Runnable r = new Runnable() {

            @Override
            public void run() {
                i = 0;
            }
        };

        if (i == 1) {
            //Single click
            handler.postDelayed(r, 250);
        } else if (i == 2) {
            //Double click
            i = 0;
            ShowDailog();
        }


    }
});
like image 176
Parag Chauhan Avatar answered Oct 12 '22 00:10

Parag Chauhan


private long lastTouchTime = 0;
private long currentTouchTime = 0;

..

         @Override
                public void onClick(View view) {

                    lastTouchTime = currentTouchTime;
                    currentTouchTime = System.currentTimeMillis();

                    if (currentTouchTime - lastTouchTime < 250) {
                        Log.d("Duble","Click");
                        lastTouchTime = 0;
                        currentTouchTime = 0;
                    }

                }
like image 28
mr.bug Avatar answered Oct 11 '22 23:10

mr.bug