How to Distinguish Between Move and Click in Ontouchevent()

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 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 ACTION_UP and actual click via onTouchListener?

Use Selector Drawable Something Like This.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- default -->
<item android:drawable="@drawable/btn_bg" android:state_focused="false" android:state_pressed="false" />

<!-- button focused -->
<item android:drawable="@drawable/btn_bg_focus_new" android:state_focused="true" android:state_pressed="false" />

<!-- button pressed -->
<item android:drawable="@drawable/btn_bg_focus_new" android:state_focused="false" android:state_pressed="true" />
</selector>

android onTouchEvent Click and Hold

you can use this code :

    yourBtn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//code that you want do when pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//code when touch stoped
}
return false;
}
});

you must remove onTouch override at the end of code.

if you want repeat a code, you can use while or a thread with handler.

ACTION_UP is not triggered for a quick/normal click on OnTouchListener.onTouch

Answering my own question:

For the first question, the problem was with the animation not the OnTouchListener.

For the second question, to differentiate between a normal click and a long click, i had to calculate how many milliseconds have passed since ACTION_DOWN was triggered when ACTION_UP or ACTION_CANCEL is triggered.

If the difference between the two events is less than 300 milliseconds it's considered a normal click, else it's a long click.

button.setOnTouchListener { view, event ->
val now = System.currentTimeMillis()
when(event?.action){
MotionEvent.ACTION_DOWN -> {
scaleDown(view)
}
MotionEvent.ACTION_UP -> {
scaleUp(view)
if(now - event.downTime < 300){
//normal click
}else{
//long click
}
}
MotionEvent.ACTION_CANCEL ->
scaleUp(view)
}
}
true
}


Related Topics



Leave a reply



Submit