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
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 am working on an android project where I need to know if user is Touch Down the screen or Touch Up the screen and I frequently need this. How can I do that?
I used on Touch Event in switch case for action Up and action down but that works for only one time. I also tried the bellow code but it gives me the the value of var(int variable) for first touch only.
my code:
int var=-1;
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if(action==MotionEvent.ACTION_DOWN)var=1;
if(action==MotionEvent.ACTION_UP)var=0;
if(action==MotionEvent.ACTION_MOVE)var=1;
return super.onTouchEvent(event);
}
I need the value of var again and again.
Since onTouchEvent is the last point of touch event traversal, it might not be called if one of the children has consumed touch event already (of if you returned false for MotionEvent.ACTION_DOWN event indicating that you are not interested in this gesture at all).
For your problem I would rather use dispatchTouchEvent() instead of onTouchEvent() and track touches there.
For more information I highly recommend getting yourself familiar with talk from Dave Smith about Android Touch System
I am writing an very simple application with following scenario:
1) Screen A have 3 button to move on other screen's.
2) Now if I hold one button(say Button 1) and perform rapid click on other button then it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.
3) and it's more weird. After move on other screen if I don't release Button 1 which was on Screen A then it still allow to perform click for rest of two button of screen A even I can see second screen.
Here it's clear launch second screen but still first screen button event working.
Any idea how can avoid such scenario.
How you are going to disable other buttons while having 1 enabled, that's an algorhytmic problem. You can try creating a boolean or control variable in your activity (and then pass the final reference of the activity to wherever you need it), or in a static context. But to answer the title of the question - you can "Cancel Touch Event" either by adding an OnTouchListener, or if you're extending class Button, you can override onTouchEvent(MotionEvent ev) method.
Using OnTouchListener will disable any previously defined touch-event behavior. You can call the actual click event from the inside by calling performClick method from your button.
//in order to use button inside OnTouchEvent, its reference must be final
//if it's not, create a new final reference to your button, like this:
final finalButton = button;
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
if(canBeClicked) {
finalButton.performClick();
return true;
}
else return false;
}
}
Overriding onTouchEvent in a class extending Button should look something like this.
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
//here we don't really need to call performClick(), although API recommends it
//we just send the touch event to the super-class and let it handle the situation.
if(activity.canBeClicked) return super.onTouchEvent(ev);
else return false;
}
One solution that I found is to disable click listener in onPause() and enable it in onResume() . Can we have better support for this?
I want to get x and y position of every touch point in my activity even in drag mode. When I implement onTouchEvent method of my activity, it does not get all of touch events. How can I do that?
Thanks in advance,
public class MyActivity extends Activity {
#Override
public boolean onTouchEvent(MotionEvent event) {
// my Code
return super.onTouchEvent(event);
}
}
This onTouchEvent method just rises when you touch around the activity.
Do you have any other View which handle touch events?
The documentation says:
Called when a touch screen event was not handled by any of the views
under it. This is most useful to process touch events that happen
outside of your window bounds, where there is no view to receive it.
So when you say you don't get any events when you are in drag mode, this implies that you're dragging some thing. So you already handling the events somewhere else and therefore the onTouchEvent() method won't be notified.
You can try to return false in the other TouchListener's onTouch() method so the event will bubble up further more.