ontouchlistener's range - android

I've got two views (one derived from SurfaceView another from ImageView) each encapsulated in a frame layout, and each view implements an ontouchlistener, which is set as the ontouchlistener of the view itself. These two views(layouts) have no intersection on the screen.
However, I cannot get these two listeners work together.
For example, if there is an ACTION_DOWN on view A (listener A), then listener B will not react (not events dispatched to B even the following motion is on view B), only listener A reacts until there is no touch events.
That is to say, even listener A is only set as the ontouchlistener for view A, it'll handle every event on the entire screen,and other listeners are inhibited.
That's strange to me, any explanations?
Can two ontouchlisteners exist together in on screen?

Of course they can, like two buttons with their own listeners. Try to set listeners to your views:
frameLayout.setOnClickListener();
imageView.setOnClickListener();
This way they should only react on events coming to their own listeners. Hope this helps.

android works as described here

Related

Android : How to detect onTouch event on a view, with touch down outside the view?

In an Android fragment, I have a GridLayout with views in it.
I would like to be able to detect when a user swipes over a view, even if he initially touches down outside of it. I've included a diagram to illustrate what I mean here :
Is there a simple way to do this (without using coordinates to see if the touch point is in the view)?
Thanks!
You solve this using View.OnTouchListeners.
Set OnTouchListeners both root view as well as your view.
Check for Events and actions ACTION_DOWN(touch event start),ACTION_UP(touch event over)
Set flags as per need for inside touch or outside touch.
basically you need capture events as well as points too.

Issue with GestureListener

I am having a gesture listener attached to a view and I have onSingleTap Event handled.
It handles it properly if I tap anywhere in the view. But say if the View is having any subview and if I am tapping the subview the event does not get triggered.
Is there a way to pass this touch from child to parent? And also the children contains BUTTONS. so if the press is on a child button it should not pass the touch to parent. Otherwise it must pass it to parent. Anyway to achieve this?
You can override the Activity's dispatchTouchEvent(MotionEvent) in order to handle the dispatching yourself.
A good handling would be to first check if it triggers the gesture in your view and if not just call super.dispatchTouchEvent(MotionEvent);
The one thing you have to care about while doing this is to keep the gestures coherent with the rest of the platform.

How to add touch event for a Layout that contains a ScrollView

The problem is the the scrolling will intercept with touch event has set to the parent layout.
Can I keep the onTouch event with the scroll in ScrollView ?
This is a very tricky part. There is an overriden method from Activity which is: public boolean onTouchEvent(MotionEvent event)
This is the general method that interprets all the touch events from the whole screen. And you could say, "ok, I can implement this and I am good to go..". And here comes the difficult part on how android works.
As you know every View has its own onTouchEvent() method that you could implement in order to add some custom implementation. So which method will listen? The ScrollView or the Activity? It appears that these touch events go from the "inside" elements to the "outside" elements. I mean parent-child relations.
Another thing to take into account is that the onTouchEvent method returns a boolean. This boolean parameter determines whether the touch event should go one level up or it is handled by the current View. Meaning that if you have a CustomViewA that implements the onTouchEvent() and CustomViewB implementing its own touch event, and the A is a child in B then the touch event would go through A first and if it is not handled it would go to B.
So basically yes it could be done. It depends on what touch event you wanted to do.
So in our case, the ScrollView returns true when the touch events are a horizontal. The activity's touch event will be handled only if the ScrollView touch event is not handled by itself then you are fine. Otherwise you have to override and implement the on touch event of scroll view and in some cases you have to return false so as for the whole layout to implement it. Good luck with the last part. I started to implement a fling effect but came up with some difficulties so I have implemented a 2 finger move with scroll view in it and it works like a charm.
This is about a week of research and experimenting and it is an overview of what I came up with. if you find anything else please let me know. Hope it helped.

Android route / bubble events because a control is consuming the onClick event

I have a custom view with an ImageView and a TextView on it and implemented the onClickListener for my custom view.
The problem is, that the ImageView is consuming the onClick-event (I just want the user be able to click on my control, no matter where). I could listen to the onClick of the Image/TextView too, but it seems dirty to me.
Is there a way to bubble / route Events in Android? Or possible another good solution?
View.onClick() event does not bubble. Two possible solutions:
Register OnCLickListener on you child views and then pass on the event by calling performClick() on parent.
Use OnTouchListener which bubbles up: just return false in child view's onTouch() method. This is more work as you have to account for touch-down & lift-up in order to emulate click.
Have you set the onClickListener in your custom view?
Set your custom view as clickable.
I don't recommend on setting any click listener in the child views.
Does it work now?

Multiple Views OnTouch Events

We are developing an application where we need to capture MotionEvents from multiple views simultaneously. When we try to do so, Android only dispatches events to the first touched view and, when touching another view simultaneously, it gives us an ACTION_POINTER_DOWN on the first view only, even if the pointer coordinates are outside its bounds and inside the other views'.
Is there any way we can get events dispatched to every touched view (in separate calls to OnTouch)?
We believe intercepting the touch events from a parent view and then manually dispatching them to each view might work, but even if it does, it would not be practical for what we are trying to achieve. Is there an easier way?
At least Android 3.0 adds support for splitting touch events between multiple Views.
Split touch events is enabled by default when AndroidManifest <uses-sdk> defines either android:minSdkVersion="11" or android:targetSdkVersion="11". For explanation See android:splitMotionEvents and android:windowEnableSplitTouch
Touch event is first catched by the view currently in focus. If you touched 1 view and have "consumed" event and are working with it, any other events will be dispatched to this view, no matter where you click.
If you return false from your touchListener (thus saying that event is not consumed) it will be delegated down to other views, but on another separate click new event will be dispatched to other view.
http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775
http://developer.android.com/guide/topics/ui/ui-events.html
While this should work automatically with Android SDK 11+, it wasn't working for my app. I was trying to capture touches in a main view, plus some views that were subviews of the main view; in that case, touches were only captured by the view "level" that received the first touch. I reorganized my view structure so that all the views were siblings and now they are capturing events as expected.

Categories

Resources