Which Android Events do I use for this task? - java

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?

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

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.

Related

Ignore touch in overlay app

i create a system overlay app using this way
but i have a problem .... when i move my button to corner of screen, i can't touch system's view like Call button in the following image
image
how can i disable any touch in my button ? ( ignore my view's touch and touch system's view )
in somewhere i find this code but it isn't work
bl.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Always visit developer.android.com first, it's really well documented with fundamental concepts.
The onTouch method will pass the event to the layer below it, if it returns false.
If you've extended a default touchable View class, you should use return super.onTouch()
Here's the link you're looking for:
https://developer.android.com/reference/android/view/View.OnTouchListener.html

How to check if and which mouse button is pressed in Swing

How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.

handling touch events in Android when using multiple views/layouts

I'm very new to Android programming, and trying to understand touch events with nested views. To start, here's a description of my app:
I have a relative layout that I've added via the GUI editor. Everything is default. I've also created a class called ClipGrid that extends ScrollView. Nested inside that, I make a HorizontalScrollView. Inside of that, I make a TableLayout and it's rows. The rows contain buttons.
The end result is a grid of buttons. It displays 4x4 at once, but can scroll either direction to display other buttons.
I call it to the screen from my main activity like this:
ClipGrid clip_grid = new ClipGrid(this);
setContentView(clip_grid);
I did that just for testing purposes, and I think I will have to change it later when I want to add other views to my relativelayout. But I think it might have implications for touch events.
in the end, I want to detect when the grid has been moved and snap the newly viewable 4x4 grid of buttons to the edge of my layout when the user lifts their finger. I'm just not sure how to go about implementing this and any help would be appreciated. Thanks.
The way touch events are handled is kind of a cascading effect that starts from the top view and goes down to the lower nested views. Basically, Android will pass the event to each view until true is returned.
The general way you could implement the onTouchEvent event of a View would be:
#Override
public boolean onTouchEvent(MotionEvent event) {
boolean actionHandled = false;
final int action = event.getAction();
switch(action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// user first touches view with pointer
break;
case MotionEvent.ACTION_MOVE:
// user is still touching view and moving pointer around
break;
case MotionEvent.ACTION_UP:
// user lifts pointer
break;
}
// if the action was not handled by this touch, send it to other views
if (!actionHandled)
actionHandled |= super.onTouch(v, MotionEvent.event);
return actionHandled;
}

Creating an interface in Android

Can someone walk me through the best way to do this? I want to make an interface that extends OnTouchListener. Instead of one meathod called onTouch(View view, MotionEvent e) i want 3 meathods; onPress() onMove() and onRelease(). The onPress meathod gets called when you press on the screen and the onMove gets called when you move your finger across the screen. onRelease gets called when you release your finger. All relevant answers are welcome.
You'd use the YourTouchListener provided by duffymo by extending your View class and adding a setYourTouchListener(YourTouchListener listener) method to this class.
You'd then override onTouchEvent(MotionEvent event) and call the relevant methods of your listener. Like so:
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
listener.onPress(this, event);
break;
case MotionEvent.ACTION_MOVE:
listener.onMove(this, event);
break;
case MotionEvent.ACTION_UP:
listener.onRelease(this, event);
break;
}
//this means that you have "used" this event. The ViewGroup will direct all further associated events straight here.
return true;
}
I would probably do essentially what CaspNZ posted, the only difference being that you shouldn't extend OnTouchListener in this case. Implementing an interface means that you must give an implementation for all of its methods, so in this case onTouch in addition to the three that you are creating, which is redundant. And of course if you still end up needing onTouch for something, you can always implement OnTouchListener in addition to YourTouchListener.
You can use GestureListener and use onFling(...) method, which gives you possibility (MotionEvent e1 and MotionEvent e2) to measure initial touch (on press) and final touch (release) and based on this you do your job. Also provides velocity of MotionEvent along x and y axis, measure pressure applied on the screen, etc. This will cut your time on writing the whole interface you are about to do.

getAction() gives only ACTION_DOWN

For an application I am writing, I want to invoke a certain action once the user lifted his finger off the screen. I understood I need to check if event.getAction() is ACTION_UP, but all I get from getAction() is ACTION_DOWN. My code looks like this:
menu = new MenuView(this);
menu.setBackgroundColor(Color.WHITE);
menu.setKeepScreenOn(true);
...
setContentView(menu);
menu.requestFocus();
...
public class MenuView extends View
{
...
public MenuView(Context context)
{
super(context);
setFocusable(true);
}
Anybody knows what is the reason for the problem?
Thank you in advance.
make sure onTouch returns true which tells the os your listener handles the touch event.
from the docs: onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
http://developer.android.com/reference/android/view/View.html#onTouchEvent%28android.view.MotionEvent%29

Categories

Resources