0

I want to display some toast when my button is clicked once and other toast when my button is clicked twice. Is there any built in library or do I need to implement custom logic?

Digital Water
  • 15
  • 1
  • 5
  • [GestureDetector](https://developer.android.com/reference/android/view/GestureDetector) does support double tap. Though using this on a button just for double tap is kind of overkill and not necessarily simpler because it require overiding touch event. [Implementing your own logic](https://stackoverflow.com/questions/4849115/implement-double-click-for-button-in-android) woudn't be hard. – Ricky Mo Jan 13 '22 at 07:55

2 Answers2

0

You can Gesture Detector for detecting double taps.

final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {

        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {

        return true;
    }
});

For more details you can check this documentation.

Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14
0

Answer given by @TaranmeetSingh is absolutely right but there is a better solution. You can use this library to do it.

How to implement

  1. Add this library -> implementation 'com.github.pedromassango:doubleClick:CURRENT-VERSION'(Version is given above).

  2. Instead of using an onClickListenerObject for onClickListener, you can use DoubleClickListener Check the example below

    Button btn = new Button(this);
    btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
         @Override
         public void onSingleClick(View view) {
    
             // Single tap here.
         }
    
         @Override
         public void onDoubleClick(View view) {
    
             // Double tap here.
         }
     });
    
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38