I have OnTouchListener and OnItemClickListener attached to ListView and for some reason OnTouchListener is getting fired but it stops OnItemClickListener from firing ? Any suggesstion on how to go about this ?
Your OnTouchListener has consumed the touch event.
From here:
check on the return value ...
If you don't wanna consume the event on OnTouchListener , just return false on onTouch(View v, MotionEvent event) as suggested by the above link.
Related
I have a parent, custom RelativeLayout which inflates two ImageViews: one content image, and a close button in the top right corner.
The issue: I cannot have both the gestures/scaling of the content image functioning AND the close button functioning.
The content image is a custom ImageView subclass (a modification of https://github.com/MikeOrtiz/TouchImageView), which has it's own OnTouchListener to allow for pinch-zooming, and responding to usual gestures.
This OnTouchListener contains the code:
private class PrivateOnTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
//....
return true;
Now, if that return is true, the scaling/gestures for the content image works, but the close button onClick is never called, whereas if it's false then the scaling/getures don't work and the onClick can be called.
I don't understand why a return function called AFTER the event is passed to the gesture detector affects (?consumes the event) whether or not the gesture detector works.
Is there a simple way of ensuring the functionality of both child ImageViews, where that return function is false but both detectors still work?
What I've tried:
Ensuring all methods in the gestureDetector return false, so that the event isn't consumed. (The scaleDetector isn't custom, so I haven't done the same there; if you think that's where the problem is let me know)
An onInterceptTouch method in the parent RelativeLayout, but I'm not sure if I implemented that correctly
(Reading around event handling to understand how it works)
I am overriding dispatchTouchEvent() method to detect each and every touch in an activity.
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
System.out.println("android test "+event.toString());
return super.dispatchTouchEvent(event);
}
But this method is catching only ACTION_UP,ACTION_DOWN and ACTION_MOVE events,not ACTION_CANCEL event. What may be the reason for this ?
#pskink's answer is correct. i.e. ACTION_CANCEL is a system event.
"ACTION_CANCEL occurs when the parent takes possession of the motion, for example when the user has dragged enough across a list view that it will start scrolling instead of letting you press the buttons inside of i.
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
I'm trying to completely disable SwipeRefreshLayout when inside the onLongClick of an item of the ListView.
I'm currently using setEnabled(false) but sometimes the swipe gesture get recognized anyways.
What could i try to stop this behaviour?
I think doing this in onLongClick is too late to lock the SwipeRefreshLayout as the user-interaction is already happening.
You could e.g. look into somehow slightly increasing the touch slop value of your ListView to suppress accidental swipes.
Look over here for discussion: Android ACTION_MOVE Threshold
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Your condition.
return true;
}
});
I have a activity where I have two imagesViews.
This to imagesViews has onClickListener, because I need to know which imageView was clicked. After when I click choosen picture I get result which picture was clicked.
I Know need the same result but I need to know where exacly I click on this image. I need precise coordinates where this imageView was clicked. I know that in onTouch method I have functions like I need.
Can I change onClick method on onTouch? Or in onClick can get precise coordinates?
There is no need for you to use the onClick event, since you can easily capture the click using the onTouch callback. A click is a sequence of one ACTION_DOWN action, several ACTION_MOVE actions and one ACTION_UP action. These can be acquired using the event.getAction() method. If you get an ACTION_DOWN event and then an ACTION_UP event - it means that the user has just clicked your View. You can also measure time spent between these events to be sure that it was a simple click, not a long one. And, of course, you can use the event.getX() and event.getY() methods to get the exact touch point. Hope this helps.
you can use onTouch() method for getting touch coordinates
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
onclick cannot do this. You can get them only from an onTouchListener.
a question with this info
I have a Main activiy, and many other activity that extends main activity. I want to track all touch events like clicking button, listview item, textView..etc in child activities .But i was unable to do that.I Implemented onTouchEvent(MotionEvent event) in main activity,
#Override
onTouchEvent(MotionEvent event)
public boolean onTouchEvent(MotionEvent event) {
System.out.println("onTouchEvent pressed");
return super.onTouchEvent(event);
}
But This function was not called.. any ideas?
Take a look at https://github.com/commonsguy/cw-advandroid/blob/master/Tapjacking/Jackalope/src/com/commonsware/android/tj/jackalope/Tapjacker.java.
It is a service that adds a transparent View to the window manager. The service also implements a onTouchListener. The listener will track all touch events while the app is running.
It is probably the easiest to implement and provides a central place to track touch events.