How can we simulate long press by touch event? or how can we calculate the time that screen is touched, all in ACTION_DOWN state?
Asked
Active
Viewed 9,796 times
4 Answers
14
I have implemented a Touch screen long click finally , thx all:
textView.setOnTouchListener(new View.OnTouchListener() {
private static final int MIN_CLICK_DURATION = 1000;
private long startClickTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
longClickActive = false;
break;
case MotionEvent.ACTION_DOWN:
if (longClickActive == false) {
longClickActive = true;
startClickTime = Calendar.getInstance().getTimeInMillis();
}
break;
case MotionEvent.ACTION_MOVE:
if (longClickActive == true) {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration >= MIN_CLICK_DURATION) {
Toast.makeText(MainActivity.this, "LONG PRESSED!",Toast.LENGTH_SHORT).show();
longClickActive = false;
}
}
break;
}
return true;
}
});
in which private boolean longClickActive = false;
is a class variable.

Soheil Setayeshi
- 2,343
- 2
- 31
- 43
-
2Thanks this lead to a solution for me. – ctapp1 Mar 26 '15 at 21:13
-
the problem is long tap is waiting for ACTION_MOVE which sometimes is not called but the long tap must be captured – sadegh saati Jun 05 '15 at 20:07
3
Try this. You don't need to find hack for this.
final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Log.e("", "Longpress detected");
}
});
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
};

guptaatul91
- 61
- 7
2
-
-
1maybe i couldn't understand you well , so hope edited answer and attached link help you more , good luck – Arash GM Jul 27 '13 at 09:33
-
It was not completely my answer but inspired me by using Boolean flags. thx :D – Soheil Setayeshi Jul 27 '13 at 09:36
1
You have to count time between ACTION_DOWN and ACTION_UP events. It's impossible to calculate this time only in ACTOIN_DOWN state, cause it's the START event of sequence of events representing TAP of LONG TAP event

Sergey Brazhnik
- 661
- 4
- 13