Handle OnTouch Event inside an Activity - android

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.

Related

Android - OnTouchListener not firing if already pressing screen

I have a RelativeLayout with a single ImageView inside of it. The ImageView has an OnTouchListener attached to it which is working perfectly fine if I just press the ImageView. The problem I'm having though is that if I press and hold in the empty space outside of the ImageView and then try to press the ImageView the touch listener doesn't fire. It seems like the layout is absorbing the touch events somehow. Any ideas on how I could fix this?
The action defined by you here is a Multi-touch gesture. Go through this doc to know how to handle this type of actions.
Also take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down.
you mout use this code:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
//your code
break;
}
return true;
}
});

Android: Long touch on Button while MotionEvent.ACTION_MOVE

Ok... in my app i update the layout on MotionEvent.ACTION_DOWN and then i check the motion event coordinates to locate my buttons. I can show a toast when finger is released on different buttons. The problem is i need a long touch on my buttons to call another action without conflicting with the MotionEvent.ACTION_UP. Implemented a long click handler but since i don't 'click' its not working. Hope you guys understand my problem.
Whats the best way to get my app working as intended?
My class implements OnTouchListener, OnGestureListener
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// UPDATE LAYOUT
break;
case MotionEvent.ACTION_UP:
// GET BUTTON X Y
if (x and y match the button location){
// DO ACTION
}else{
// DO NOTHING
}
// CHANGE LAYOUT TO INITIAL STATE
break;
case MotionEvent.ACTION_MOVE:
break;
}
return false;
mybutton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// DO STUFF
return true;
}
});
}
just try to return false in your onTouch(...) method and use onLongClickListener(...) as usual

Determine when touch down happens and then when it is released

I thought my code would work, but I guess not. I'm just trying to get it to read when the phone is touched. An animation will happen while its held. Then I would like it to determine when it is released so the animation will stop. My code looks like this with toasts as fillers until I get it working.
ImageView image = (ImageView)findViewById(R.id.pImage);
image.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
Toast.makeText(PalsPage.this, "Is DOWN", Toast.LENGTH_SHORT).show();
break;
}
case MotionEvent.ACTION_UP: {
Toast.makeText(PalsPage.this, "Is UP", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
Any help would be appreciated. Thanks
I think you are getting only the "Is DOWN" Toast.
The problem is in the return value.
You have to return truefor the code to work. otherwise the ACTION_UP event will not come to this view's listener.
you should return true so your image view keeps the events. i imagine your getting the first toast but not the second.

GridView problems with touchlisteners

I'm trying to create a custom GridView but i'm having troubles with the touch listeners.
What i want to do:
Create a GridView with custom Views.
Longpress on an item so it becomes 'editable'.
Drag the view horizontal or vertical to move it's position in the GridView.
Here's where i'm having trouble:
I'm implementing GestureDetector.OnGestureListener for the longpress functions, because for some reason using the gridview.setOnItemLongClickListener() isn't working when overriding the onTouchEvent() of the GridView itself (Which i need for the dragging part). So everything is fine at this point. Now i only need to know when the longpress is finished. So i though: "Well this shouldn't be hard." I couldn't have be more wrong. I've fiddled around for quite some time with this and it looks like using different touch events isn't helping me :/
When stepping through the onTouchEvent() i noticed that only 1 action is given: MotionEvent.ACTION_DOWN. So what am i doing wrong? i need the MotionEvent.ACTION_UP...
Found the culprit:
i was doing something like this
#Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
boolean retValue = gestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE :
onMove();
break;
case MotionEvent.ACTION_UP :
onUp();
break;
case MotionEvent.ACTION_CANCEL:
onCancel();
break;
}
return retValue;
}
i think retValue was always returning false so no other events were triggered.
this fixed the issue:
#Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
gestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE :
onMove();
break;
case MotionEvent.ACTION_UP :
onUp();
break;
case MotionEvent.ACTION_CANCEL:
onCancel();
break;
}
return true;
}

Android how to tell a finger is down

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.

Categories

Resources