4

I have a custom listview, in which items are scroll horizontal. I want to perform single touch and longpress for items for 5 sec for listview items. How to do this. How can i increase longpress time interval for listview items to 5 sec.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Manish Kumar
  • 71
  • 1
  • 6

4 Answers4

2

This replicates onLongPress more accurately because it does not wait for the user to lift their finger before executing. Was written specifically for ViewPager, but should be able to apply similar logic.

// long press duration in milliseconds
public static final int LONG_PRESS_DURATION = 2000;

private boolean mIsTouching = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == event.ACTION_DOWN) {
        mIsTouching = true;
    } else if (event.getAction() == event.ACTION_UP) {
        mIsTouching = false;
    }

    return super.onTouchEvent(event);
}

@Override
public void onLongPress(MotionEvent event) {

    // subtracts the system set timeout since that time has already occured at this point
    int duration = LONG_PRESS_DURATION - getLongPressTimeout();

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mIsTouching) {
                do something...
            }
        }
    }, duration > 0 ? duration : 0);
}
Denver AK
  • 21
  • 1
  • 3
  • Where's the `onLongPress(MotionEvent)` method? I can't find it in `ViewPager`. Do you mean the `onLongClick(View view)` in `View.OnLongClickListener`? – Tim Kist Oct 05 '17 at 09:50
  • Thanks. Worked for me, although I had to remove the getLongPressTimeout() from your solution. – TomV Nov 13 '17 at 12:02
  • This can be done more simply by starting the postDelayed in `onTouchEvent`/ACTION_DOWN. There, it is not necessary to subtract getLongPressTimeout. Do not need `onLongPress`. – ToolmakerSteve Oct 05 '18 at 21:39
2

Here is my implementation: (You don't need to release to shot the action)

Handler longTouchHandler= new Handler();
static final int longduration=10000;//10000ms press duration
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == event.ACTION_DOWN) {
        longTouchHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.w("Longpress","longpressed");
            }
        }, longduration);
        return true;
    }
    else if (event.getAction() == event.ACTION_UP) {
        longTouchHandler.removeCallbacksAndMessages(null);
        Log.w("Longpress","cancelled");
    }

    return super.onTouchEvent(event);
}
1

You can setOnItemLongClickListener, then you can set the view you touch a OnTouchListener, you can record the time when ACTION_DOWN and the time ACTION_UP, so you can calculate if the time is more than 5 sec between ACTION_DOWN and ACTION_UP.

Piyush
  • 18,895
  • 5
  • 32
  • 63
juemuren4449
  • 105
  • 3
  • 2
    IMO that would be an unnatural usecase because we usually wait for the popup or any changes before we release the finger, the user does not typically count `1.2.3.4.5` then release. – Enzokie Dec 26 '16 at 12:00
  • To clarify Enzokie's comment: if you wait for ACTION_UP to calculate the time, user has no way of knowing when they have held it long enough. Instead, the action should be triggered on a delay after ACTION_DOWN, if the finger is still touching. See Denver's answer. – ToolmakerSteve Oct 05 '18 at 21:44
1

You cant change the delay. It is hardwired in the android framework. I also came across same problem days ago.

You can use setOnTouchListener for it manually.

Example :

private long then;
private int longClickDur= 5000; //5 seconds


//you can use any view you want
ImageView imageView = (ImageView) findViewById(R.id.longclick_view);
imageView.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          then = (long) System.currentTimeMillis();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          if ((System.currentTimeMillis() - then) > longClickDuration) {
            /* Long click behaviour will go here*/
            Toast.makeText(context, "yay, long click", Toast.LENGTH.SHORT);
            return false;
          } else {
            /* LONG CLICK FAILED*/
            Toast.makeText(context, "TRY AGAIN", Toast.LENGTH.SHORT);
            return false;
          }
        }
        return true;
      }
    });
Amit Bhandari
  • 3,014
  • 6
  • 15
  • 33