Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DoubleTap in android [duplicate]

Tags:

android

I need to create a small text area.Within that text area when i double click,it will move to next activity.How could i do this?

like image 756
sanjay Avatar asked Jan 26 '11 12:01

sanjay


People also ask

How do I handle double tap on Android?

Implement the onTouch(view,event) method. (In double tap the key is to detect two ACTION_DOWN and ACTION_UP events. For this we will have to calculate the time duration between two successive down-up events).

Does Android 11 have double tap?

If you followed Google's work on Android 11, you'd likely be familiar with the back tap gesture the company had included in its early developer previews. The Android 11 double-tap gesture allowed you to tap the back of your Pixel device to do things like launch the phone's camera app and control media playback.

Do Android phones have back Tap?

The back tap gesture has been present in iPhones for quite some time now. Android manufacturers have done their best to mimic it, but the feature still remains a novelty for most devices. Well, this is no more.


3 Answers

If you do the setup right, the OnDoubleTapListener, within the GestureListeneris very useful. You dont need to handle each single tap and count time in between. Instead let Android handle for you what a tap, a double-tap, a scroll or fling might be. With the helper class SimpleGestureListener that implements the GestureListener and OnDoubleTapListener you dont need much to do.

findViewById(R.id.touchableText).setOnTouchListener(new OnTouchListener() {     private GestureDetector gestureDetector = new GestureDetector(Test.this, new GestureDetector.SimpleOnGestureListener() {         @Override         public boolean onDoubleTap(MotionEvent e) {             Log.d("TEST", "onDoubleTap");             return super.onDoubleTap(e);         }         ... // implement here other callback methods like onFling, onScroll as necessary     });      @Override     public boolean onTouch(View v, MotionEvent event) {         Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");         gestureDetector.onTouchEvent(event);         return true;     } }); 

Note: I tested around quite a while to find out, what the right mixture of return true and return false is. This was the really tricky part here.

Another note: When you test this, do it on a real device, instead of the emulator. I had real trouble getting the mouse fast enough to create an onFling event. Real fingers on real devices seem to be much faster.

like image 78
jboi Avatar answered Oct 09 '22 09:10

jboi


A better alternative is to create a lightweight Abstract Class

public abstract class DoubleClickListener implements OnClickListener {

    private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds

    long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long clickTime = System.currentTimeMillis();
        if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            onDoubleClick(v);
            lastClickTime = 0;
        } else {
            onSingleClick(v);
        }
        lastClickTime = clickTime;
    }

    public abstract void onSingleClick(View v);
    public abstract void onDoubleClick(View v);
}

And use it like

 view.setOnClickListener(new DoubleClickListener() {

        @Override
        public void onSingleClick(View v) {

        }

        @Override
        public void onDoubleClick(View v) {

        }
    });
like image 43
Zar E Ahmer Avatar answered Oct 09 '22 11:10

Zar E Ahmer


very simple logic use below code

    boolean firstTouch = false;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(event.getAction() == event.ACTION_DOWN){
                if(firstTouch && (Helper.getCurrentTimeInMilliSeconds() - time) <= 300) {
                    //do stuff here for double tap
                    Log.e("** DOUBLE TAP**"," second tap ");
                    firstTouch = false;

                } else {
                    firstTouch = true;
                    time = Helper.getCurrentTimeInMilliSeconds();
                    Log.e("** SINGLE  TAP**"," First Tap time  "+time);
                    return false;
                }
            }
            return true;
    }
like image 32
Hits Avatar answered Oct 09 '22 10:10

Hits