I understand calling onTouchEvent from views to get the location of the last touch as a motion event. How the heck can I tell that a fingure is down on the screen and has not been lifted and when the finger is lifted?
For instance there is onKeyDown and onKeyUp for use when you are dealing with keyboard input. So how can I find out when on fingureUp happens?
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_MOVE:
// Etc...
}
}
Then just fill in the cases with what you want to happen on these events.
onTouchListener got a parameter which is typed MotionEvent. Where you can getAction() to know if it is UP or DOWN or else.
Related
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;
}
How can I detect multitouch events? The code im trying is:
ImageView im = (ImageView) findViewById(R.id.imageView1);
im.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.e("case MotionEvent.ACTION_POINTER_DOWN","case MotionEvent.ACTION_POINTER_DOWN");
break;
case MotionEvent.ACTION_DOWN:
Log.e("case MotionEvent.ACTION_DOWN","case MotionEvent.ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
Log.e("case MotionEvent.ACTION_UP","case MotionEvent.ACTION_UP");
break;
case MotionEvent.ACTION_MOVE:
Log.e("case MotionEvent.ACTION_MOVE","case MotionEvent.ACTION_MOVE");
break;
}
return false;
}
});
It detects the first touch and the output in the log cat is MotionEvent.ACTION_POINTER_DOWN. How to know whether a second touch has taken place?
There are many ways to do it, so u can choose one of many here some link have write a complete application to handle multi touch.
multitouch
multi-touch-handling
You should add follwing line to imageView1 in your xml layout file:
android:longClickable="true"
This will enable long clicks and other events will be received too.
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.
Im Trying to handle an OnTouch Event inside an activity But I am unable to Handle MotionEvent.ACTION_UP Action. here is my code:
boardView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
showToastNotification("ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
showToastNotification("ACTION_UP");
break;
}
return false;
}
});
Why Is That?
Thanks in advance
Kobi
The "onTouch" event has to return "true" istead of "false".
Do you see the "ACTION_DOWN" toast? Is it just the "ACTION_UP" toast which does not show?
Could be (Guess) When ACTION_DOWN happens, the Toast appears and the Activity is not in control of ACTION_UP. Or.. as #C0deAttack said, it might be behind the scenes. Use ACTION_DOWN notification as Toast.short and ACTION_UP as Toast.long .. OR you could use LogCat.