OnTouch event, finger on screen but not moving - android

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;
}
});

Related

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 SurfaceView: Better way to tell if screen is touched (java)

I need a way to tell if there is at least one finger touching the screen. I had no problem doing this in LibGDX but now I can't do it without it. This is how I have it setup:
public static boolean screenTouched = false;
//This methods is run at 60FPS by the main thread (it's the main game loop)
public static void update(){
//Run other updates...
screenTouched = false;//Called after other updates
}
public boolean onTouchEvent(MotionEvent e){
screenTouched = true;
}
By the way, this is all in the main activity class.
This seems like it would work fine, right? It doesn't, onTouchEvent(MotionEvent e) is only called by android when the user's finger moves on the screen and doesn't care about whether they are just touching it or not. The problem with this is that if the user touches the screen and keeps their finger in place while still touching the screen, onTouchEvent will not be called and screenTouched will stay false. Anyone know a way around this to be able to tell if the screen is being touched even if the users finger isn't moving?

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.

problem with onclick android

i've been searching the web but i find no answer to my question. I have a button, when you press it, it will play a sound. The problem is that when you touch a button on the screen it goes to the onClickListener() only after the button have been released. I need it to run the listener when the button is pressed not when it's released, because this cause a delay when playing the sound. I tried onTouchListener() and it didn't work either, because the sound get's played every time i move the finger over the button. I tried onKeyDown() but it won't work for screen buttons. Any ideas? Some help will be appreciated.
Thanks
You can use OnTouchListener and test the event action:
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) { // ...
}
}
Maybe you can still use your implementation for onTouchListener(). Just set a boolean flag for when the user starts touching the button, then while it is set true (meaning the user hasn't released the button yet) do not play the sound. When the user releases the button (ACTION_UP), set the flag back to false. This would mean you are ready to play the sound again.

get continues reading from continues touch

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;
}

Categories

Resources