android: How to increase long press time for listview items in android?

4k views Asked by At

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.

4

There are 4 answers

2
juemuren4449 On

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.

3
Amit Bhandari On

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;
      }
    });
0
Levente Gulyás On

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);
}
3
Denver AK On

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);
}