get continues reading from continues touch - android

I want to get continues reading from a continues touch down event.
I mean that I want to get event every 0.01 sec.
I've tried to use GestureDetector and I got down, show and long events during a continues
touch. After the long event I don't get any events.
Please help...

You need to listen for TouchMotion events in the Activity's onTouch() method.

Where are you logging the events? The onTouch() handler should be called whenever the user touches the screen. You can do a switch case on the event to determine what the user is doing:
switch(event) {
case MotionEvent.ACTION_DOWN:
// When user first touches screen
break;
case MotionEvent.ACTION_MOVE:
// When user is dragging, or continuous touch
break;
case MotionEvent.ACTION_UP:
// When user stops touching
break;
}

Related

ACTION_UP not working when click three button in the same time

I have 3 buttons, I want to handle some events when I touch those 3 buttons, I have considered the events of motion event, everything runs normally however if I touch those 3 buttons at the same time, the event The ACTION_UP of the 3 buttons will not be called instead the ACTION_CANCEL event. I don't know the cause of this and how can I call ACTION_UP in this case?
Here in my code:
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
System.out.println("down");
break;
case MotionEvent.ACTION_UP:
System.out.println("up");
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_CANCEL:
System.out.println("cancel");
break;
}

Send data to arduino as long as the button is being pressed

I want to send data to an arduino mega 2560 as long as a button is being pressed and when that button is released it will stop sending informations. I am using onTouchListener with MotionEvent constants. But when I run this on my phone I press the button and it sends data even though after a while I release it. Where am I being wrong here?
switch (v.getId()) {
case R.id.left1: // check what button is pressed
while(event.getAction() == MotionEvent.ACTION_DOWN) {
bt.sendData("1"); // while pressing the button it sends data
}
if(event.getAction() == MotionEvent.ACTION_UP) {
// when it stops, do nothing
}
break;
}
return true;
Your problem is in infinite loop while(event.getAction() == MotionEvent.ACTION_DOWN) that you start upon receiving the first event.
OnTouchListener is called for each event that is dispatched to view, down and up are separate events and event does not change while being processed.
So to solve your problem - you need to send data from a separate thread.
Start it on ACTION_DOWN and also have a flag that will be modified on ACTION_UP to indicate thread to exit.
You have to set the flag of bt.sendData to false when button is released which seems to be absent in your code.
It's like you open tap for water but forget to close the tap when you are finished. Hope it helps

Android - Is it safe to assuming MotionEvent.ACTION_UP will 100% mean a user has touched view?

I have a view (WebView to be specific). In that view, I have something like:
setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//user has clicked
}
}
});
will this 100%, ALWAYS guarantee me that the user has tapped/clicked on the view? If not, under what cases would this not guarantee me a click??
I want to intercept all user "clicks". think of "clicking" like you would "click" a button, but just on a mobile device. Imagine this code being called 100 million times by different devices
MotionEvent.ACTION_UP is when you lift up your finger from the screen. You can be sure the user has touches a view once the onTouch is called.
I think that while MotionEvent.ACTION_UP won't always be called when the touch ends (as mentioned here: ACTION_UP not always called?), it's certainly safe to assume that a user has touched the screen. That's how the API describes it, IMO:
A pressed gesture has finished, the motion contains the final release
location as well as any intermediate points since the last down or
move event.

Android: OnTouchListener.OnTouch not called in multiTouch

I implemented multitouching buttons in my app, by definyng and attaching my version of OnTouchListener (and onTouch method) to all my buttons.
All works perfectly, except for one thing which seems to belong to standard behaviour of the listener. I hope to be able to explain it without posting code since it's long.
If I click and hold a button, the onTouch method is called, as expected, and application sees button clicked.
If I click and hold a second one, the onTouch method (related to first button) is called, as expected, and application sees button clicked.
If I release only second one, if my finger on the first button stays ABSOLUTELY motionless, the onTouch event is not called and the second button is not logically released. The onTouch method gets called only if I move at least a pixel first finger, and only in that moment the second button gets released.
I know that it's very difficult to keep a finger exactly in the same position for more than one millisecond :-), but sometimes it happens..I had to spend some time in order to understand the reason for this behaviour, and I didn't find a solution, have you got any suggests to workaround this problem?
Thank you very much.
Try this inside your onTouch
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
float xDiff = Math.abs(curr.x-start.x);
float yDiff = Math.abs(curr.y-start.y);
if (xDiff < 5f && yDiff < 5f) {
//insert here your code when button is click
}
break;
It's help for me avoid inaccuracy of push.

OnTouch event, finger on screen but not moving

I'd like to perform an action when the user let his finger on a View more than 1 sec.
With OnTouch, I can access to ACTION_UP,ACTION_DOWN and ACTION_MOVE.
The problem is that if the user has his finger on the screen and doesn't move his finger, ACTION_MOVE is not triggered and thus I can't perform my action.
I precise that I'd like to perform my action after 1sec but while the user still have its finger on screen.
In fact I'd need to sense that the user has his finger on the screen even if he's not moving it.
Thanks for your help.
You can Make Use of the TimerTask class and schedule a Task which will run after a specific time.
Here is an Example.
In case the user has lifted his finger before 1000 ms then just cancel the task scheduled upon ACTION_UP event.
Or you could use a longclickListener :)
View myView = findViewById(R.id.myView);
wv.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//DO SOMETHING HERE
return true;
}
});

Categories

Resources