2

The problem that i am having is I am using a touch listener for the onTouch event it seems that the touch is being called more than once when i just touch it once. The following is my code

final TextView tv = (TextView)findViewById(R.id.TextView01); tv.setOnTouchListener(new View.OnTouchListener() { int count1 =0; int count2=0; public boolean onTouch(View arg0, MotionEvent event) { Log.d("Code777","Touch is being called finger"); int i = event.getPointerCount(); if(i==1 ) { if(count1==0) { Log.d("Code777","Touched with 1 finger"); count1++; count2=0; } } if (i==2 ) { if(count2==0) { Log.d("Code777","Touched with 2 fingers"); edit.append(tv.getText()); count2++; count1=0; } } return true; } }); 

Am i doing something wrong ?? It prints the log more than 3-4 times for both single touch and double touch

The problem updated problem is that both the events are getting fired now

 if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) { Log.d("Code777","2 finger touch"); return true; }else if(event.getAction() == MotionEvent.ACTION_DOWN) { Log.d("Code777","Touched with 1 finger"); return true; } 

2 Answers 2

7

You're code will execute during every touch event. The first time it activates, it's likely do to an ACITON_DOWN event (when the user first touches the screen). The second time, it is likely do to an ACTION_UP event (when the user lifts the finger from the screen). Likewise, if you were to swipe your finger around the screen, the same code will execute many times for an ACTION_MOVE event. You have to check for the types of touches that it is. Do something like this:

switch(event.getAction()){ case MotionEvent.ACTION_DOWN: // Do something that should only happen when the user first touches the screen break; case MotionEvent.ACTION_MOVE: // Do something that should only happen when the user is touching the screen break; case MotionEvent.ACTION_UP: // Do something that should only happen when the user stops touching the screen break; } 

EDIT:

Multitouch in Android is odd at best. Not all devices can handle it. Not all device can handle more than x number of touches, etc. If you want to handle DOWN cases for individual fingers, then you can use the ACTION_POINTER_X items. If you were to have this series of events like so:

1. User touches screen 2. User touches screen with second finger 3. User lifts finger 1 4. User touches screen with finger 1 5. User lifts finger 2 6. User touches screen with finger 2 7. User lifts both finger 1 and finger 2 8. User touches screen with only finger 2 9. User touches screen with finger 1 

The events that will be fired will be like this:

1. ACTION_DOWN 2. ACTION_POINTER_2_DOWN 3. ACTION_POINTER_1_UP 4. ACTION_POINTER_1_DOWN 5. ACTION_POINTER_2_UP 6. ACTION_POINTER_2_DOWN 7. ACTION_UP (also, one of the pointers actions will fire depending on which finger was lifted first) 8. ACTION_DOWN 9. ACTION_POINTER_2_DOWN 

And so on.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the answer it worked as you suggested but i have another problem now , if i am using TOUCH_DOWN it is not detecting the double touch, is there any way to get around this ??
ACTION_DOWN is for only the first initial touch of all. You can check for ACTION_POINTER_2_DOWN for the second. After which, if finger 2 is down, you life finger one, then put finger 1 back down, ACTION_POINTER_1_DOWN will fire.
I don't understand your issue.
only double touch event is getting executed the Action_pointer_1_Down is not executed
The ACTION_POINTER_1_DOWN event will only be executed if the first finger that touches the screen is lifted and put down and the second finger never stops touching the screen. Basically think of it as an assignment. A finger touches the screen->If pointer1 is not assigned, assign the touch to pointer1->else if pointer2 is not assigned, assign the touch to pointer2. Multitouch in Android isn't very intuitive (though supposedly easier in Honeycomb; I don't have much experience in it).
1

An onTouchListener sends you multiple actions via MotionEvents. You can get the action for the current event with MotionEvent.getAction().

What you notice here is most likely that you get one ACTION_DOWN (a finger has been placed on the display) event followed by some small ACTION_MOVE events when you move the finger(s) slightly. In the end you will get ACTION_UP (a finger has been lifted) You can filter these out by ignoring events with the wrong action.

For example just return from onTouch() when a move event occured.

public boolean onTouch(View arg0, MotionEvent event) { Log.d("Code777","Touch is being called finger"); if(event.getAction() == MotionEvent.ACTION_MOVE) { Log.d("Code777", "Move event detected, ignored!"); return false; } // Further processing .. } 

or use a switch to differentiate between all relevant events.

See the MotionEvent class documentation for a list of possible actions. .

6 Comments

That works great. thanks but i have another problem, if i am using TOUCH_DOWN it is not detecting the double touch, is there any way to get around this ??
You mean you don't get a ACTION_DOWN event when you place the second finger on the display?
yes, the action event is not being called. I hope the way you test that is by getting the event.getPointerCount(); or is there some other way to detect multi touch ?
Basically that event should get fired. But it depends on the device, some have a few problems with multitouch. Which device are you using exactly?
I am using the basic droid but i dont think it is a device specific because in my previous revision of the code it was detecting the multitouch
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.