Android, is there an OnRelease functions? - java

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.

Related

Is it possible to set FLAG_NOT_TOUCHABLE on an activity, but also close the activity when a touch occurs?

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

Detect swipe over view

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

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.

How to get "onTouchEnd" event in Android?

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.

How to differentiate between sliding and clicking 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.

Categories