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.
android: How to increase long press time for listview items in android?
4k views Asked by Manish Kumar At
4
There are 4 answers
3
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
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
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);
}
You can
setOnItemLongClickListener
, then you can set the view you touch aOnTouchListener
, you can record the time whenACTION_DOWN
and the timeACTION_UP
, so you can calculate if the time is more than 5 sec betweenACTION_DOWN
andACTION_UP
.