Skip to content Skip to sidebar Skip to footer

Change Long Click Delay

I am listening for a View's long click events via setOnLongClickListener(). Can I change the long click delay / duration?

Solution 1:

This is my way for set duration to long press

privateintlongClickDuration=3000;
privatebooleanisLongPress=false;

numEquipeCheat.setOnTouchListener(newView.OnTouchListener() {
        @OverridepublicbooleanonTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                isLongPress = true;
                Handlerhandler=newHandler();
                handler.postDelayed(newRunnable() {
                    @Overridepublicvoidrun() {
                        if (isLongPress) {
                            Vibratorvibrator= (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
                            vibrator.vibrate(100);
                            // set your code here// Don't forgot to add <uses-permission android:name="android.permission.VIBRATE" /> to vibrate.
                        }
                    }
                }, longClickDuration);
            } elseif (event.getAction() == MotionEvent.ACTION_UP) {
                isLongPress = false;
            }
            returntrue;
        }
    });

Solution 2:

AFAIK, no. It is hard-wired in the framework via getLongPressTimeout() on ViewConfiguration.

You are welcome to handle your own touch events and define your own "long click" concept. Just be sure that it is not too dramatically different from what the user expects, and most likely the user will expect what all the other apps use, which is the standard 500ms duration.

Solution 3:

I defined an extension function in Kotlin inspired by @Galoway answer:

fun View.setOnVeryLongClickListener(listener: () -> Unit) {
    setOnTouchListener(object : View.OnTouchListener {

        privateval longClickDuration = 2000Lprivateval handler = Handler()

        overridefunonTouch(v: View?, event: MotionEvent?): Boolean {
            if (event?.action == MotionEvent.ACTION_DOWN) {
                handler.postDelayed({ listener.invoke() }, longClickDuration)
            } elseif (event?.action == MotionEvent.ACTION_UP) {
                handler.removeCallbacksAndMessages(null)
            }
            returntrue
        }
    })
}

Use it like this:

button.setOnVeryLongClickListener {
    // Do something here
}

Solution 4:

This was the simplest working solution I found to this restriction:

//Define these variables at the beginning of your Activity or Fragment:privatelong then;
privateint longClickDuration = 5000; //for long click to trigger after 5 seconds

...

//This can be a Button, TextView, LinearLayout, etc. if desired
ImageView imageView = (ImageView) findViewById(R.id.desired_longclick_view);
imageView.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          then = (long) System.currentTimeMillis();
        } elseif (event.getAction() == MotionEvent.ACTION_UP) {
          if ((System.currentTimeMillis() - then) > longClickDuration) {
            /* Implement long click behavior here */
            System.out.println("Long Click has happened!");
            returnfalse;
          } else {
            /* Implement short click behavior here or do nothing */
            System.out.println("Short Click has happened...");
            returnfalse;
          }
        }
        returntrue;
      }
    });

Solution 5:

This is what I use. It is similar to Cumulo Nimbus' answer, with two notable differences.

  1. Use the already stored event values for down time and current time. Aside from not duplicating effort, this has the nice effect of making the listener usable for multiple views at the same time without tracking start times for each individual event.
  2. Check view.isPressed to ensure the user has not moved away from the view during the touch event. This mimics the default system behavior for onClick and onLongClick.

long longPressTimeout = 2000;

@Override
public boolean onTouch(View view, MotionEvent event) {
    if (view.isPressed() && event.getAction() == MotionEvent.ACTION_UP) {
        long eventDuration = event.getEventTime() - event.getDownTime();
        if (eventDuration > longPressTimeout) {
            onLongClick(view);
        } else {
            onClick(view);
        }
    }
    returnfalse;
}

If the view is not normally clickable you will need to call view.setClickable(true) for the view.isPressed() check to work.

Post a Comment for "Change Long Click Delay"