I am trying to catch double-tap events using OnTouchListener. I figure I would set a long for motionEvent.ACTION_DOWN, and a different long for a second motionEvent.ACTION_DOWN and measure the time between the two of them and then do something with it. However, I am having a hard time figuring out exactly how to approach this. I am using switch cases to pick up multitouch events, so I'd rather not try and retool this all to implement GestureDetector (and unfortunately it is impossible to implement both ontouchlistener and Gesturedetector simultaneously). Any ideas would help greatly:
i.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
long firstTouch = System.currentTimeMillis();
///how to grab the second action_down????
break;
Intercept touch events in a ViewGroup. The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup , including on the surface of its children.
After the Math. abs() calls, you're essentially testing if their finger is farther down the screen than it is across the screen. Store the initial down coordinates as member variables and set them during ACTION_DOWN . You declared two floats (touchX and touchY) inside the onTouchEvent method.
Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.
In your class definition:
public class main_activity extends Activity
{
//variable for counting two successive up-down events
int clickCount = 0;
//variable for storing the time of first click
long startTime;
//variable for calculating the total time
long duration;
//constant for defining the time duration between the click that can be considered as double-tap
static final int MAX_DURATION = 500;
}
Then in your class body:
OnTouchListener MyOnTouchListener = new OnTouchListener()
{
@Override
public boolean onTouch (View v, MotionEvent event)
{
switch(event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
startTime = System.currentTimeMillis();
clickCount++;
break;
case MotionEvent.ACTION_UP:
long time = System.currentTimeMillis() - startTime;
duration= duration + time;
if(clickCount == 2)
{
if(duration<= MAX_DURATION)
{
Toast.makeText(captureActivity.this, "double tap",Toast.LENGTH_LONG).show();
}
clickCount = 0;
duration = 0;
break;
}
}
return true;
}
}
This was adapted from an answer in: DoubleTap in android by https://stackoverflow.com/users/1395802/karn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With