How to differentiate between sliding and clicking in android? - java

In pure Java there is MouseInputListener which I can use to work with those 2 events.
How do I do it with Android?
If I implement both events then only one is fired (onClickListener) and not the other.
Updated:
The question is not about detecting the finger movement.
I have a View (ImageView for example). I need to detect the click on this view which is onClickListener() and finger movement on this view (i.e. press, move then release the finger).
The problem here is that only onClickListener() is called and MotionEvent handler is not caught.
I need to be able to differentiate those 2 events as the main event should be finger movement and onClickListener() should just say "Don't click this view. Spin this view."
Hopefully this is more clear.

OnClickListener and OnTouchListener kinda obstruct each other, since they both consume the MotionEvents that get caught on the View.
Basically you can write a single OnTouchListener that checks for both things. You'll get supplied with the MotionEvent as an argument. Check it's action via MotionEvent.getAction(), e.g. if it equals MotionEvent.ACTION_DOWN (the user put the finger on the display). If the user releases the finger at approx. the same position (ACTION_UP), you may want to interpret that as a click. Otherwise interpret the positions that you get with the ACTION_MOVE event as a gesture.
But the framework already has some classes that do this interpreting work for you, check out the SimpleGestureDetector class and it's SimpleOnGestureListener. That has some callbacks for common events, e.g. onSingleTapConfirmed() and onFling(). All you need to do is supply the MotionEvents from your OnTouchListener to the GestureDetector.

Related

Android find difference between two ImageView

I am developing a puzzle game like find differences on images in Android (Java).
So how can i know, where user touched on image, and is it right place where user touched.
make some CustomView extends View and override onTouchEvent method. or handle all touch events in Activity by overriding dispatchTouchEvent. you can obtain precise point of touch/action with getX() and getY() methods of MotionEvent (which is an object passed to both methods mentioned above), now its your turn to do some math and calculate which item has been touched (by default Activity is dispatching touch events to its layout e.g. inflated from XML)

Gesture Detection for Specific View

Added a spaceView that doesn't cover all of the layout. And I want gesture detection work for only that view.
Couldn't do much but here is my codes:
space = findViewById(R.id.space);
this.gDetector = new GestureDetectorCompat(this, this);
gDetector.setOnDoubleTapListener(this);
The onTouchEvent in Android lets you do so. You can add whatever you want to do in that event.
As per Android Studio's official site, onTouchEvent() is described as ...
When a user places one or more fingers on the screen, this triggers the callback onTouchEvent() on the View that received the touch events. For each sequence of touch events (position, pressure, size, addition of another finger, etc.) that is ultimately identified as a gesture, onTouchEvent() is fired several times.
Here's the official link to you for further help
Hope this helps...

Fling and scroll movements

My View implements onGestureListener to let me be informed when a gesture is done by the user, either if it is a scroll or a fling movement.
When the pointer is moved slowly on the screen, I receive onScroll events as expected.But when the pointer is moved faster, I get several onScroll events before getting the onFling event. As the onFling event passes the event associated with the "up" event, I suppose it is only triggered after the user takes his finger away from the screen.
Ans this causes a not natural behaviour for my application...
My question is : is it possible to suppress all the preceding onScroll events when the move executed by the user is clearly a fling movement?
Problem solved, but the solution doesn't look very clean : in the "onFling" function, I cancel every treatments realized in the "onScroll" function and I only take the "onFling" event into account if one of the velocities is greater than 1000 in absolute value.

How to detect draglike behavior without ACTION_DOWN and ACTION_UP

My problem concerns onTouch. As far as I can tell, there is no way to detect an ACTION_DOWN for one view, and than detect the ACTION_UP for another view at the same onTouch event or swipe, as every onTouch event is linked to one view.
I have a view (A) with some graphics, and on EVENT_DOWN the coordinates are detected and make a "popup" that consists of a view (B) that ownes some more views ("buttons"). B's position and size may vary form time to time. I would prefer to detect ACTION_UP on one of B's children (the "buttons").
I guess using onInterceptTouchEvent is no good. After all, B is not A's parent. Its the other way around.
Using androids drag-and-drop functionality seems a bit too much. Looks like it's intended for actually dragging graphics and transfering data. Dragging invisible graphics around to detect the finger leaving the screen is not very elegant.
Another way would be detecting B's children's positions (and size), every time the popup is shown, but that is not very smooth either.
What is the best way to detect a view A's ACTION_DOWN, and then its child B's (or it's childrens) ACTION_UP?
Or are there other ways to detect these events?
I did struggle with something very similar for a day or two, and I finally concluded that as far as I know onInterceptTouchEvent is the only way to go. I just created a container view 'C' for A and B, and then when A received an ACTION_DOWN, I instructed 'C' to intercept all further events in the swipe, test the coordinates against B's known location, and then forward the event to 'B' through a custom method if the ACTION_UP tested positive against B's bounds (be careful manually passing MotionEvents from one class to another, as the coordinates will be all wrong).
Not a very satisfying solution, but it was the only thing I could think of to do.
Also just in case you're using it, android 3.0+ has some drag/drop helper methods which I'm sure are very helpful, though I was developing for 2.x so they were useless to me.

Android, is there an OnRelease functions?

Say I want a dialog to .show() when the user Touches a certain element and .hide() once he releases it.
I found how to make the OnTouchListener. But is there any sort of OnReleaseListener?
Thanks!
Thats actually included in the OnTouchListener. It gives you a MotionEvent on the callback,
use MotionEvent.getAction() and check if it equals MotionEvent.ACTION_UP. That means the user released the finger.
Equally you can check for ACTION_DOWN to differentiate the two.
onTouchListener returns you a TouchEvent object, which contains the current touch action, that can be retrieved by calling event.getAction(). There are some actions, one of them are ACTION_DOWN and ACTION_UP: first tells you, that the user has touched some view, and second tells you that user has taken his finger off a view. Means onRelease will be onTouch with the ACTION_UP action. Hope this helps.

Categories