I am currently using dispatchTouchEvent to grab touch events, is there an easy way to distinguish between a click and a "drag" stylt gesture?
DispatchTouchEvent is called with a MotionEvent parameter. Method getAction within MotionEvent can return
ACTION_DOWN
ACTION_MOVE
ACTION_UP
ACTION_CANCEL
Then set on ACTION_DOWN flag isClick. If there is ACTION_MOVE clear isClick flag.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
isClick = true;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (isClick) {
//TODO Click action
}
break;
case MotionEvent.ACTION_MOVE:
isClick = false;
break;
default:
break;
}
return true;
}
set up a threshold limit.When you move the pointer within a small range make it recognized as a click or else a movement
Related
I have a problem with a second touch on screen and I would like to disable it. Primaly, object that has been touch if first finger is released is teleporing to second finger (MotionEvent.ACTION_POINTER_DOWN).
Is it possible to disable this event?
Try add touch listener and catch and cancel ACTION_POINTER_DOWN/UP:
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.d("ButtonCode", "ACTION_POINTER_DOWN");
return true;
break;
case MotionEvent.ACTION_POINTER_UP:
Log.d("ButtonCode", "ACTION_POINTER_UP");
return true;
break;
default:
break;
}
return false;}
There is this https://github.com/03uk/RippleDrawable
beautiful implementation to create a Ripple Effect on non L devices.
As you can see it is a Drawable that implements OnTouchListener.
The problem is that the Drawable intercepts ALL touch events, and therefore cannot be used in ListViews and similar.
How to avoid it?
I think the problem is in here https://github.com/03uk/RippleDrawable/blob/master/app/src/main/java/dreamers/graphics/RippleDrawable.java
and specifically here:
#Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
onFingerDown(v, event.getX(), event.getY());
v.onTouchEvent(event); // Fix for views, to handle clicks
return true; // fix for scroll, when ACTION_UP & ACTION_CANCEL not come
case MotionEvent.ACTION_MOVE:
onFingerMove(event.getX(), event.getY());
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
onFingerUp();
break;
}
return false;
}
I am facing problem during detecting touch events. Its working fine without scrolling. But after scroll finished when I am removing finger not able to receive motion up event.I am using following code.
linearMain.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do Something
Log.d("Touch", "Touch down");
// Now Set your animation
slidingDrawerMos.startAnimation(slideOutAnimation);
slidingDrawerReviews.startAnimation(slideOutAnimation);
slidingDrawerMos.setVisibility(View.GONE);
slidingDrawerReviews.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_MOVE:
// Do Something
Log.d("Touch", "Touch move");
break;
case MotionEvent.ACTION_UP:
// Do Something
Log.d("Touch", "Touch up");
slidingDrawerMos.setVisibility(View.VISIBLE);
slidingDrawerReviews.setVisibility(View.VISIBLE);
// Now Set your animation
slidingDrawerMos.startAnimation(slideInAnimation);
slidingDrawerReviews.startAnimation(slideInAnimation);
break;
case MotionEvent.ACTION_CANCEL:
Log.d("Touch", "Touch cancel");
break;
default:
break;
}
return true;
}
});
your_control.setMovementMethod(new ScrollingMovementMethod());
and
findViewById(R.id.your_control).getParent()
.requestDisallowInterceptTouchEvent(false);
I'm trying to use multitouch as a method to press 2 things simultaneously.
Here's my Code:
TouchHandle.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
int actions = event.getActionMasked();
switch (actions)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
SendKeyCode(Server, "keydec.down");
case MotionEvent.ACTION_POINTER_UP:
SendKeyCode(Server, "keydec.up");
case MotionEvent.ACTION_UP:
}
return true;
}
});
The problem is that when I touch the screen, the server is retrieving "keydec.down" and "keydec.up" even thought I haven't removed my finger yet from the touchscreen. So the View TouchHandle is detecting an ACTION_POINTER_DOWN and ACTION_POINTER_UP when my finger touches the screen even if I haven't pulled my finger up yet. Any thoughts?
int actions = event.getAction();
switch (actions)
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_POINTER_DOWN:
SendKeyCode(Server, "keydec.down");
break;
case MotionEvent.ACTION_POINTER_UP:
SendKeyCode(Server, "keydec.up");
break;
case MotionEvent.ACTION_UP:
break;
}
You should use breaks between cases. And i also think that these MotionEvent constants might work with event.getAction() try that one.
I'm trying to make a small android Jump and Run game but my problem is that i can't configure the event ACTION_UP right. Here my Code:
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("OTE", "down");
touchDownTrue = true;
break;
case MotionEvent.ACTION_UP:
Log.d("OTE", "UP");
touchDownTrue = false;
break;
}
}
the case MotionEvent.ACTION_UP is never called and i don't know why, the same happens if i use ACTION_CANCEL
After I insert return super.onTouchEvent(event); at the end of the method (onTouchEvent must return a value) your code works for me, when I put it in a blank main activity.
You should probably return true instead of breaking in those cases because you are responding to the event.