Make OnTouchListener update all the time - android

I have an Android applciation which I want it to draw circles around
I used OnTouchListener.
The problem is, when the users "holds" the circle in place, it doesn't update the action.
How can I know if the user's finger is on,when he doesn't move it, with the OnTouchListener

You'll have to check if what kind of event is triggering onTouchEvent.
#Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do Nothing
return true;
case MotionEvent.ACTION_MOVE:
//Do Something
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
//Do Nothing
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
More Information about MotionEvents can be found here.

You can know that by using
MotionEvents:
Reed more here: MotionEvents
ACTION_DOWN is for the first finger that touches the screen. This starts the gesture. The pointer data for this finger is always at index 0 in the MotionEvent.
ACTION_POINTER_DOWN is for extra fingers that enter the screen beyond the first. The pointer data for this finger is at the index returned by getActionIndex().
ACTION_POINTER_UP is sent when a finger leaves the screen but at least one finger is still touching it. The last data sample about the finger that went up is at the index returned by getActionIndex().
ACTION_UP is sent when the last finger leaves the screen. The last data sample about the finger that went up is at index 0. This ends the gesture.
ACTION_CANCEL means the entire gesture was aborted for some reason. This ends the gesture.
here is a good answer to read StackOverFlow

Filter touches by what kind they are:
boolean onScreen = false;
#Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX(); //X coord of the touch
float eventY = event.getY(); //Y coord of the touch
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// User has touched the screen
onScreen = true;
return true;
case MotionEvent.ACTION_MOVE:
// Finger is being dragged on the screen
onScreen = true; //Not required, just for clarity purposes.
break;
case MotionEvent.ACTION_UP:
// User has lifter finger
onScreen = false;
break;
default:
return false;
}
if(onScreen) {
//Finger is on the screen
}
return true;
}

Related

How to record (x,y) of a gesture at a fixed frequency?

I am now trying to record some (x,y) of a gesture. Now I have referred to some material and found that OnTouch function can help us get the (x,y) of the point that we touch down. However, I now need to move my finger and record the (x,y) of the points in the path at a fixed rate. How can I do that?
Try with the following code it will help you.
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//invoke when you will touch down finger on screen
break;
case MotionEvent.ACTION_MOVE:
//invoke when you will move finger on screen
fingerMove(event.getX(), event.getY());
break;
case MotionEvent.ACTION_UP:
//invoke when you will up finger from screen
break;
}
return true;
}

Implement both swipe and longclick on list item?

I need to implement both Longclick and Left & right swipe on a list view and get the listitem on which the action was performed. This method seemed really promising.
Problems:
1.ACTION_MOVE is fired only once at the start so the diff is really minimal
2.If i use a default in the switch i get the last location but onClick or onLongClick is never fired. Here is what i tried.. Is it possible to fire a fake ACTION to cause itemClick/itemlongclick to execute.
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.NONE;
Log.i("MyTags","Down Event");
Log.i("MyTags",String.valueOf(downX)+","+String.valueOf(downY));
return false; // allow other events like Click to be processed
case MotionEvent.ACTION_MOVE:
upX = event.getX();
upY = event.getY();
Log.i("MyTags","Move Event");
Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
moveEnabled=true;
return false;
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();
Log.i("MyTags","UP Event");
Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
return false;
default:
upX = event.getX();
upY = event.getY();
Log.i("MyTags","Default Event");
Log.i("MyTags",String.valueOf(upX)+","+String.valueOf(upY));
if(moveEnabled)
{
diffX=downX-upX;
diffY=downY-upY;
abs_X=Math.abs(diffX);
abs_Y=Math.abs(diffY);
moveEnabled=false;
if((abs_X>abs_Y)&(abs_X>MINIMUM_X))
{
if(diffX>0)
{
mSwipeDetected=Action.LEFT;
Log.i("MyTags","Left Swipe");
event.setAction(MotionEvent.ACTION_UP);
return false;
}
else if(diffX<0)
{
mSwipeDetected=Action.RIGHT;
Log.i("MyTags","Right Swipe");
event.setAction(MotionEvent.ACTION_UP);
return false;
}
}
}
return false;
}
}
I kind of used a hack to fix my issue.What happens is since the events are consumed, i never get the result in the onitemclick event. What i have done is to fire my own Down and Up events once a swipe action is detected and during those fake events i return false so that its passed down to the itemclick listener. We need to be a bit careful here as the event seems to read from the result 2 times , for this i turn the listener on and off , there by containing the output.
ACTION_MOVE:
//when swipe is detected fire a fake down event
event.setAction(MotionEvent.ACTION_DOWN);
v.dispatchTouchEvent(event);
ACTION_DOWN:
if(fake down event)
//set some flags & dispatch fake event
event.setAction(MotionEvent.ACTION_UP);
v.dispatchTouchEvent(event);
return false
else do regular handling
ACTION_UP:
if fake down event
dispatch fake up event
else ..
here 'v' is the view on which the event has to be fired , in my case its the listview.
Since we dont have it right-off it has to be passed in through the constructor of the swipeDetector class. As follows..
ListView v;
SwipeDetector(ListView lv)
{
this.v=lv;
}

Android Touch Events Pointer Down

Hi I am trying to detect when 2 fingers are touching the screen:
case MotionEvent.ACTION_POINTER_2_DOWN: {
twoFing=true;
return true;
}
the problem is that:
public static final int ACTION_POINTER_2_DOWN
is depreceted, the doc says:
*Constant for getActionMasked(): A non-primary pointer has gone down.
Use getActionIndex() to retrieve the index of the pointer that changed.
The index is encoded in the ACTION_POINTER_INDEX_MASK bits of the unmasked action returned by getAction().*
but I don't understand how to use it... How could I detect that there are 2 pointers? ActionUP and DOwn always say there is only one pointer if I try getPointerIndex()
thanks a lot
EDIT: I post here the full code to be more clear about the problem. My code is working BUT as the ACTION_POINTER_2_DOWN is a deprecated value I want to replace it by something else but I don't know how.
#SuppressWarnings("deprecation")
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN : {
Log.i(TAG, "Action Down");
downX = event.getX(0);
downY = event.getY(0);
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX(0);
upY = event.getY(0);
float deltaX = downX - upX;
float deltaY = downY - upY;
Log.i(TAG, "Action UP deltaX="+deltaX+", deltaY="+deltaY);
// swipe vertical?
if(Math.abs(deltaY) > MIN_DISTANCE && twoFing){
twoFing=false;
// top or down
if(deltaY < 0 )
{
if(slide.zoom==1)
slide.zoom=0;
Log.i(TAG, "Going Down zooming in");
//return true;
}
if((deltaY > 0) )
{
if(slide.zoom==0)
slide.zoom=1;
Log.i(TAG, "Going up zoomig out");
//return true;
}
return true;
}
// swipe horizontal?
if(Math.abs(deltaX) > MIN_DISTANCE && !twoFing){
// left or right
if(deltaX < 0) { this.slideToTheLeft(); return true; }
if(deltaX > 0) { this.slideToTheRight(); return true; }
return true;
}
return false;
}
case MotionEvent.ACTION_POINTER_2_DOWN: {
twoFing=true; //inform that the touch was made with 2 fingers
Log.i(TAG, "Action Second pointer down");
return true;
}
}
return false;
}
I'm working on pointer stuff myself right now. You want to switch as follows
switch(event.getAction() & MotionEvent.ACTION_MASK);
case MotionEvent.ACTION_POINTER_DOWN:
ACTION_DOWN is about the first finger down.
ACTION_POINTER_DOWN is about the second finger.
See http://www.vogella.com/articles/AndroidTouch/article.html for a fairly clear description.
MotionEvent.ACTION_DOWN is only for the first pointer. GetActionMasked() will return MotionEvent.ACTION_POINTER_DOWN for subsequent pointers.
Did you try with getPointerId(index) with different values for index ?
See the difference between Id and Index:
Index vs. ID
At a higher level, touchscreen data from a snapshot in time may not be
immediately useful since touch gestures involve motion over time
spanning many motion events. A pointer index does not necessarily
match up across complex events, it only indicates the data’s position
within the MotionEvent. However this is not work that your app has to
do itself. Each pointer also has an ID mapping that stays persistent
across touch events. You can retrieve this ID for each pointer using
MotionEvent.getPointerId(index) and find an index for a pointer ID
using MotionEvent.findPointerIndex(id).
The best solution should be to save the state of all the pointers and update/compare them you receive a new TouchEvent.
Check this example.

How do I detect if a user has touched any part of the screen other than the inside of an EditText?

I am guessing this means making the whole screen touchable. How exactly is that done?
and secondly calculating if an X,Y is within the EditText.
override the onTouchEvent in the activity you want to implement this....and get the X, Y co-ordinates using event.getX() and event.getY()
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
but i suggest you must search thoroughly before posting a question.

"Live" get(x) and get(Y) values (for MotionEvents) ****UPDATE****

I was wondering how to get accurate, live get(x) and get(y) values for a MotionEvent? What is happening is that when I touch a specific area on the screen, I tell an action to happen.
The problem is that once I touch the screen and take my finger off, it still thinks my finger is at the same location (since that was the last location I touched). So when I have more than one Down event (for multitouch) it throws everything off. Is there a way to reset the X and Y values so when I let off the screen, they go back to 0 or null (or whatever)? Thanks
What you are describing isn't a problem. You the programmer are responsible for keeping track of the touch locations and what they mean. If you care about motion you need to keep track of the previous touch and the current touch each time a touch occurs. I find something like this works great:
public int x=-1,y=-1,prevX=-1, prevY=-1;
public boolean onTouch(View v, MotionEvent event)
{
prevX = x;
prevY = y;
int x = (int)event.getX();
int y = (int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// there is no prev touch
prevX = -1;
prevY = -1;
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
Just catch the MotionEvent.ACTION_UP or ACTION_MOVE event, depending on when the action needs to happen (since you want it live, you should use the ACTION_MOVE event). You should handle setting external variables when ACTION_DOWN occurs, and handle the results on ACTION_MOVE or ACTION_UP.

Categories

Resources