What is the name of a event that is called once he user stops touching the screen for the View object?
onTouchEvent fires with a type of ACTION_UP.
You can check here to see all available events: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP
The one for you is ACTION_UP.
Hope this helps!
Update:
Moreover when you move outside the View without moving up your finger, ACTION_CANCEL is called. So you should handle both.
Related
I want to make a touch on a button in a layout and also that touch is done by program using injectinputevent method, so that button is clicked even without touching it.
You can use the performClick method on that button.
Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
You can call if from another method:
your_button.performClick();
I would like to set FLAG_NOT_TOUCHABLE on a transparent activity to allow interaction with the activity below. Is there any way to close the transparent activity when a user touches the screen while these flags are set?
I do not believe you'd be able to determine if the user touched the screen if FLAG_NOT_TOUCHABLE is set. Your Activity will simply not receive any touch events at all.
Instead, what you can do is set an onTouchListener to the root View, then return false indicating that you do not want to handle the touch. This way, you'll always receive the first touch event (ACTION_DOWN) which allows you to call finish() and go.
yes, you can.
// not any touch event
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
I want a view to be able to detect whether the user swipes (or rather moves his/her finger) over it: Swipes should be detected no matter whether they begin inside or outside the view, same goes for the end of the touch event.
As far as I can see, adding an OnTouchListener on the view won't work for this kind of gesture, as onTouch isn't called given that the touch event starts outside the view.
Any help on that subject is highly appreciated.
Please take a look at this question, should have the answer for you:
Detect which view your finger is sliding over in Android
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.
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.