Android: detect driving over an ImageView with a finger? - android

With which onTouchListener Event i can detect " Driving with the finger over an ImageView"?

You need to implement View.OnTouchListener.
Then override onTouch() in your Activity, and in ACTION_DOWN and ACTION_UP, check the coordinates of the touch point from the MotionEvent to see if they are in the area covered by the ImageView.

That's conflicts principle of giving responsibility to objects. ImageView should be aware of it's touch events, not the activity or fragment.

Related

Cancel onTouchEvent when GestureDetector's onDoubleTap triggers

A View for freehand writing implements onTouchEvent for the drawing operations.
Besides, a GestureDetector recognises double tap events to wipe the drawing. However, when the double tap occurs, onTouchEvent triggers an ACTION_UP, which draws an unwanted point on the view.
In iOS I handled these kind of problems with the touchesCancelled method, is there something similar in Android?
I solved by handling onDoubleTapEvent instead of onDoubleTap.

capturing both clicks and drags in android

I am overriding both onTouch and onClick. Each is supposed to result in different actions (very important). I don't want a touch to be both a touch and a click: it must be either-or. I am not having much luck. How do I get my app to discriminate between onTouch and onClick? Right now, either I get onTouch by itself or I get both onTouch and onClick together (based on whether I change onTouch to return true or false).
An easy solution is to just use onTouch(). Look for ACTION_UP and ACTION_DOWN. Store the position of the touch in ACTION_DOWN, and in ACTION_UP if the distance between the touch and the stored touch are < X then fire a custom click event, otherwise fire the drag event.

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.

Detect multitouch with Ontouch listener Android

I currently have an imageview element that upon being touched, that tests if the action is an actiondown event, and if it is, it gets the coordinates of the touch with getraw(x or y) and then carries out an action based on those coordinates. How would I implement this to get two sets of coordinates from a two finger multitouch event?
Take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down after the first ACTION_DOWN triggers. You can use methods like getActionMasked() to assist you in determining which finger events are related to.
MotionEvent Docs
HTH
This is a nice example

touchlisteners and textviews

Actually I'm trying to implement an ontouchlistener into my android (1.5) application.
therefore i implemented the "ontouchlistener" into the class, and then i put my code into the:
public boolean onTouchEvent (MotionEvent e){
//code
}
The problem is, that if I am dragging over an (e.g.) Spinner or EditTextView than this Method isn't called. The only way to solve this which i figured out is to add an ontouchlistener to each of this views manually:
((Spinner)(findViewById(R.id.spinner1))).setOnTouchListener(tl);
((Spinner)(findViewById(R.id.spinner2))).setOnTouchListener(tl);
(tl is the ontouchlistener)
So isn't there a way to catch the touch event before it gets to each of those views?
thanks in advance
Ripei
What exactly do you want to do - do you want to:
Detect whole gestures in some
Activity.
Detect touches only for a given
View
If you want the former:
Take a look at this example:
Fling gesture detection on grid layout.
Basically, you redirect all registered touch events to a GestureDetector.
It understands what exactly is the user input (single tap, double tap, scroll, etc.) and calls the corresponding callback in a SimpleOnGestureListener, which you should implement.
If you want the latter:
You can either:
Override the View's onTouchEvent().
Implement and hook onTouchListener (if you want to get the touch before it's dispatched to it).
If you want to implement onTouchListener, you'll be doing your work not in the onTouchEvent() method, but in the interface's onTouch() method.
Hope that helps.

Categories

Resources