I have a Java class that pertains to mouse listeners that I'm wanting to convert over to my Android app but can't quite find the necessary events.
My Java app makes use of the following methods:
- mouseClicked
- mousePressed
- mouseReleased
I'm wanting to do something similar however not with click events but touch events. I have come across OnTouchListener and did an override on the onTouch method.
What are the alternatives to mousePressed and mouseReleased?
Edit - (updated after Peter's response)
Are the following events correct:
- ACTION_DOWN : mouseClicked
- ACTION_MOVE : mousePressed
- ACTION_UP : mouseReleased
EDIT - 2 Example Source
My Activity doesn't have any OnTouchListener at the moment because I was hoping I could keep all the touch logic in my View.
View:
/*Inside my View - Is it proper to do onTouch logic here?
Or should I be doing this from the Activity?*/
public class myView {
public boolean onTouch(MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//draw arrow when screen is simply touched
break;
case MotionEvent.ACTION_MOVE:
//Do Logic
break;
case MotionEvent.ACTION_UP:
//Do Logic
break;
}
}
}
The reason I am doing the logic in my View is because I have some variables that I would like to grab directly rather than creating multiple extra get methods.
Am I able to do it like this? Or will I have to override the onTouch method in my Activity and do the logic there?
2 Answers 2
All touch events (down, up, move, multitouch) are handled via 'onTouch'. See this tutorial: http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775
2 Comments
ACTION_DOWN is equivalent to mouseClicked, ACTION_MOVE is equivalent to mousePressed, and ACTION_UP is equivalent to mouseReleased?ACTION_DOWN equals mousePressed and ACTION_MOVEequals something like dragging.If you want to register clicks on a View, implement and add an OnClickListener to it.
When you want to register touch events you need to implement the OnTouchListener and add it to that View.
7 Comments
ACTION_DOWN equals mousePressed, what would be the equivalent of mouseClicked. From my understanding mouseClicked would be just tapping the devices screen and mousePressed would be until the screen is released (in case the user moves the touch). Does ACTION_DOWN account for both mouseClicked and mousePressed?onTouch method in the View? Or overriding it from the Activity?ACTION_DOWN does not account for clicked and pressed but for mousePressed. The OnClickListeneris what the mouseClicked does represent. Although you could create your own click event with a combination of ACTION_DOWN ACTION_UP and some threshold of time and screen coordinates.onTouch isn't working. I currently have it as a method inside my View. Do I need to set it from my Activity instead?