Skip to content Skip to sidebar Skip to footer

Simulate Long Press By Touch Events

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?

Solution 1:

I have implemented a Touch screen long click finally , thx all:

textView.setOnTouchListener(newView.OnTouchListener() {

    privatestaticfinalintMIN_CLICK_DURATION=1000;
    privatelong startClickTime;

    @OverridepublicbooleanonTouch(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) {
                longclickDuration= Calendar.getInstance().getTimeInMillis() - startClickTime;
                if (clickDuration >= MIN_CLICK_DURATION) {
                    Toast.makeText(MainActivity.this, "LONG PRESSED!",Toast.LENGTH_SHORT).show();
                    longClickActive = false;
                }
            }
            break;
        }
        returntrue;
    }
});

in which private boolean longClickActive = false; is a class variable.

Solution 2:

For calculating touch count you can get getPointerCount() of your event like here

and for Long click maybe this helps

Edit: and hope this link help you determining getting touch duration

Solution 3:

Try this. You don't need to find hack for this.

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
 publicvoidonLongPress(MotionEvent e) {
  Log.e("", "Longpress detected");
 }
});

public boolean onTouchEvent(MotionEvent event) {
 if (gestureDetector.onTouchEvent(event)) {
  returntrue;
 }
 switch (event.getAction()) {
  case MotionEvent.ACTION_UP:
   break;
  case MotionEvent.ACTION_DOWN:
   break;
  case MotionEvent.ACTION_MOVE:
   break;
 }
 returntrue;
}
};

Solution 4:

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

Post a Comment for "Simulate Long Press By Touch Events"