Skip to content Skip to sidebar Skip to footer

Android: How To Increase Long Press Time For Listview Items In Android?

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 incre

Solution 1:

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 millisecondspublicstatic final int LONG_PRESS_DURATION = 2000;

private boolean mIsTouching = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

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

    return super.onTouchEvent(event);
}

@Override
publicvoidonLongPress(MotionEvent event) {

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

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        publicvoidrun() {
            if (mIsTouching) {
                do something...
            }
        }
    }, duration > 0 ? duration : 0);
}

Solution 2:

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.

Solution 3:

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 :

privatelong then;
privateint 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();
        } elseif (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);
            returnfalse;
          } else {
            /* LONG CLICK FAILED*/
            Toast.makeText(context, "TRY AGAIN", Toast.LENGTH.SHORT);
            returnfalse;
          }
        }
        returntrue;
      }
    });

Solution 4:

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
            publicvoidrun() {
                Log.w("Longpress","longpressed");
            }
        }, longduration);
        returntrue;
    }
    elseif (event.getAction() == event.ACTION_UP) {
        longTouchHandler.removeCallbacksAndMessages(null);
        Log.w("Longpress","cancelled");
    }

    return super.onTouchEvent(event);
}

Post a Comment for "Android: How To Increase Long Press Time For Listview Items In Android?"