0

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?

asked Aug 9, 2011 at 19:24

2 Answers 2

3

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

answered Aug 9, 2011 at 19:31
Sign up to request clarification or add additional context in comments.

2 Comments

Ironically I was reading that article when I noticed I had a reply on SO. So if I understand it correctly, ACTION_DOWN is equivalent to mouseClicked, ACTION_MOVE is equivalent to mousePressed, and ACTION_UP is equivalent to mouseReleased?
Actually ACTION_DOWN equals mousePressed and ACTION_MOVEequals something like dragging.
1

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.

answered Aug 9, 2011 at 20:58

7 Comments

Correct, that is what I'm doing. However, I'm looking for which specific events are closest to the three methods in Java. If 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?
Also, do you suggest creating the 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.
I followed a guide and have come with an attempt at it. However, in the emulator if I click with a mouse will it count it the same as a touch? If so, my onTouch isn't working. I currently have it as a method inside my View. Do I need to set it from my Activity instead?
I think now it's about time to see your Listener implementations and where you are using them in code.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.