Prevent touch within small time interval in Android - android

I am facing an issue if user touches multiple button within a small time interval(less than 1 second). I want only one of them to be executed. Similar issue with touch and swipe as well.
I have tried below solutions:
Use a boolean flag and set inside onClickListener. If this flag is then directly return from onClickListener. This approach does not work for me as it is not very deterministic to decide correct place to unset the flag.
Check time difference between two consecutive onClicListener and ignore 2nd if it is less than some threshold value. Issue with this approach that it is not very deterministic and threshold may vary for devices. Also if I keep it very high and device will appear to be very slow.
What I want is 2nd touch to not even register if first touch onClickListener is not completed. Are there other ways to control the touch events in Android?

What you're looking for is a debouncer / debounced click listener. You can find implementation details by searching using these terms, with some examples here - https://medium.com/swlh/android-click-debounce-80b3f2e638f3 - and here - Avoid button multiple rapid clicks.

Related

Android Studio: MotionEvent - touch tracking

I'm new to Java but handling it quite well because of my years of programming with python (basic concepts carry from language to language).
I have a practice project set up and the only propose of this project is for me to learn how to do things the java/android studio way, logic wise.
So logically, the last python based api I used, there was a way to issue your own ID to touches and keep track of those touches; therefore you knew if a touch still existed or not.
That's where I am right now. I know how to get the touch ID and then get the Index that holds the touch data. Basically I'm using a For Loop on the motion event to process all touches to the screen.
Here's the thing....
Lets say I have 10 touches to process and what I do in a loop is basically collect the info of the touches and store them in a hashtable, so I can then later process all the touches in the hashtable.
So as I pull a touch data from the hashtable to process, I want to first check to see if the touch still exits. I'm guessing once a touch enters the "UP" state, it goes out of scope after that because a touch is over after release.
Sure I can just check the "Action" to see if it's equal to "UP" but if the touch doesn't exits anymore, that would raise and exception...right?
What I thought about doing was, calling "getPointerCount" again, looping through and getting the IDs again and see if the ID of a touch in my hashtable still exits in the MotionEvent and if the ID exists and the "Action" is still equal to "DOWN", then process the touch. Otherwise, if the ID does not exists or the "ACTION" is equal to "UP", do a release button event if needed or just delete the touch from my hashtable because it doesn't exits anymore.
I can't help but get the feeling of redundancy though when considering that approach.
Is there a way or method I don't know about that allows you to check if a touch still exits before you process actions relating to it?
I was thinking just now.... Well, kind of guessing that, if you have five touches that hit the screen and one of the touches triggers the opening of a menu and that button had higher priority over the other four touches, you could simply ignore the other for touches for that frame and open the menu and in the next frame of the app, if the other four touches are still down, you can deal with them then accoring to their priority..... But then again, the touch could still exists in the next frame but the "Action" may be "UP" or "MOVE" instead of down, thus allowing one to write code for a release or move event. With this thought, storing touch data in a hashtable is really not needed then, granted a touch this is on action "UP" will go out of scope in the very next frame (I'm guessing).
......hum......
Maybe I'm just over thinking it.
Forgive any "typos", eyes are that super any more.
The code I wrote was correct but I wasn't getting the results because I was using MotionEvent.getAction().
For anyone new to Android Studio and its APIs, do not use MotionEvent.getAction(). Instead, use MotionEvent.getActionMasked()...otherwise, multi-touch will not work correctly.
For me, only the 1st touch was registering when I was doing a double button press test. I thought my code was flawd but all was working correctly when I used MotionEvent.getActionMasked().
Turns out, MotionEvent.getAction() just doesn't return an Action...other data is attached to it, which throws off your code.
MotionEvent.getActionMasked() returns the Action only.

Passing clicks events from floating app to covered app

I have a floating app which works perfectly.
I am using OnTouchListener to catch events since I need to use the GestureDetector for swipes etc.
My only problem is that sometimes I wish to ignore certain events on the view.
In this case the view is invisible but not "gone" because I need it to accept certain gestures but not others.
I can't seem to be able to do that.
Returning false from "onTouch" simply doesn't work.
I checked that by experiment by disabling the GestureDetector and simply always returning false just to see what would happen. Result was nothing going through.
Is it even possible to pass a click through to a covered app?
Due to security reasons it's not possible to record and pass a click below (essentially allows building a keylogger).
Best you can do is have your floating window small enough to start the touch but not cover too much of the screen below.

Android - touch two buttons at same time

What is the best way to touch two buttons at the same time? I am working on an app that has buttons, like a D-pad and a jump button, to move your character around. Right now I am just using normal buttons and handling them with an OnClickListener. I am having problems when I am running and need to jump at the same time, or if I am running to the right, then want to go left without having to pick my finger up. I know this is possible because it works greats on game like Sonic CD and some others. Any help would be greatly appreciated.
OnClick fires only on release. Instead use the touch event handlers so that when they touch occurs, you get the events. However, note that not all devices have multitouch, and thus not all of them will be able to handle the double-touch case correctly. They will provide touch events, but not two of them. Also note that you may receive multiple "pointers" within a touch event, and will have to decide which is "yours" for each button if that matters.

onClick event in android webview too slow

I've got the feeling that javascript itself is pretty fast in an andorid webview, but there is a long delay between touching an element and the onclick event beeing fired.
I could imagine that this is a feature if you navigate between pages - you first see the highlight on the element, and then you see the effect (navigation). But for applications, this is too slow.
Is there a way to change this behavior?
Or is there maybe another event I should go for, like an onHover or onTouch? Something which fires way before the onClick?
This is a known 'issue' related to 300ms due to user tapping/zooming at display:
http://updates.html5rocks.com/2013/12/300ms-tap-delay-gone-away
In the near future, this seems to be solved, in static sized layouts, adding information at header. But the actual webkit webview version does not have/consider this option.
In my case, the solution was use Tappy lib:
https://github.com/filamentgroup/tappy
Just import it, and bind to each element that has an click event, and you will see the difference.
Just one more point: I adjusted the timer to 100000ms (default:1000ms) to avoid event repetition in some specific cases.
I guess I've found the answer...
If you take a look at jQuery mobile, you'll find a tap event. This works instantly - exactly as I need it.
Looking deeper into the source, I've found that the tap event uses a mousedown ....

Multi Touch Events in android

Hi Here i want to solve the problem regarding multitouch in android. Is this possible to fire events of 2 buttons together if this is possible then how can i do this. please help regarding this. In my game application i want to do something after pressing two buttons together. How can i do this.
I don't think that it is possible to aggregate two events to a single one oob. But it shouldn't be to hard to determine if two buttons were pressed simultaneously by using a OnTouchListener you bind to the parent element of the two buttons.
When an event is thrown and the onTouch() method is called. You got a bunch of arguments related to the event, like what kind of event it was (Action_Down, Action_Move, Action_Up etc.). You also get the information if the action was performed by the first oder by the second finger so this should give you the option to decide whether both buttons were touched.
Here you can find a nice tutorial about multi touch support on Android.

Categories

Resources