How to Detect a Click in an Ontouch Listener

How can I detect a click in an onTouch listener?

Masoud Dadashi's answer helped me figure it out.

here is how it looks in the end.

viewPager.setOnTouchListener(new OnTouchListener() {
private int CLICK_ACTION_THRESHOLD = 200;
private float startX;
private float startY;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
if (isAClick(startX, endX, startY, endY)) {
launchFullPhotoActivity(imageUrls);// WE HAVE A CLICK!!
}
break;
}
v.getParent().requestDisallowInterceptTouchEvent(true); //specific to my project
return false; //specific to my project
}

private boolean isAClick(float startX, float endX, float startY, float endY) {
float differenceX = Math.abs(startX - endX);
float differenceY = Math.abs(startY - endY);
return !(differenceX > CLICK_ACTION_THRESHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHOLD);
}
}

How to distinguish between move and click in onTouchEvent()?

Here's another solution that is very simple and doesn't require you to worry about the finger being moved. If you are basing a click as simply the distance moved then how can you differentiate a click and a long click.

You could put more smarts into this and include the distance moved, but i'm yet to come across an instance when the distance a user can move in 200 milliseconds should constitute a move as opposed to a click.

setOnTouchListener(new OnTouchListener() {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if(clickDuration < MAX_CLICK_DURATION) {
//click event has occurred
}
}
}
return true;
}
});

How to detect onTouch release event in Android after object is set invisible?

u can try this

  buttonA.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
YourMethodForButtonBGoesVisible();
YourMethodForButtonAGoesGone();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
YourMethodForButtonAGoesVisible();
YourMethodForButtonBGoesGone();
}
return true;
}
});

I don't know if when the button A gone this will detect the MotionEvent.ACTION_UP but u can try.

singletap touch detect in Ontouch method of the view

I also ran into the same problem recently and ended up having to implement a debounce to get it working. It's not ideal, but it's pretty reliable until I can find something better.

View.onClickListener was much more reliable for me, but unfortunately I need the MotionEvent from the OnTouchListener.

Edit: Removed the excess code that would cause it to fail here

class CustomView extends View {

private static long mDeBounce = 0;

static OnTouchListener listenerMotionEvent = new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if ( Math.abs(mDeBounce - motionEvent.getEventTime()) < 250) {
//Ignore if it's been less then 250ms since
//the item was last clicked
return true;
}

int intCurrentY = Math.round(motionEvent.getY());
int intCurrentX = Math.round(motionEvent.getX());
int intStartY = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalY(0)) : intCurrentY;
int intStartX = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalX(0)) : intCurrentX;

if ( (motionEvent.getAction() == MotionEvent.ACTION_UP) && (Math.abs(intCurrentX - intStartX) < 3) && (Math.abs(intCurrentY - intStartY) < 3) ) {
if ( mDeBounce > motionEvent.getDownTime() ) {
//Still got occasional duplicates without this
return true;
}

//Handle the click

mDeBounce = motionEvent.getEventTime();
return true;
}
return false;
}
};
}

OnClickListener and OnTouchListener collision issues

I'd suggest you use only the OnTouchListener and handling the click case there using something like:

    ImageButton floatingIcon = (ImageButton) findViewById(R.id.floatingIcon);
gestureDetector = new GestureDetector(this, new YourGestureListener());
floatingIcon.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (gestureDetector.onTouchEvent(arg1)) {
// A single tap has been made - treat it like a click and consume the event
return true;
} else {
// The MotionEvent is not a click event, handle and decide if the event is consumed
}
return false;
}
});

private class YourGestureListener extends SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
return true;
}
}

Detecting both Click and Touch for an ImageView on android

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}

@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.e("Touch up", "Touch up");
return super.onSingleTapUp(e);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}

return false;
}
}
}

How to detect if click event is finished in android?

For that you need to use an onTouchListener => documentation

Refer this tutorial on how to use this.

Example:

myView.setOnTouchListener(
new View.OnTouchListener() {
public boolean onTouch(View myView, MotionEvent event) {
int action = event.getAction();
if (action==MotionEvent.ACTION_MOVE)
{
myView.setBackgroundColor(Color.BLUE);
}
if (action==MotionEvent.ACTION_UP)
{
myView.setBackgroundColor(Color.RED);
}
if (action==MotionEvent.ACTION_DOWN)
{
myView.setBackgroundColor(Color.YELLOW);
}
// TODO Auto-generated method stub
return true;
}
}


Related Topics



Leave a reply



Submit