I am trying to find out the Action Name that is being performed by the mouse. I tried
String act=String.valueof(event.getAction());
it returns me integer 3.
Can anyone guide me with the list of integer associated with motionevent in android.
It's not clear if anyone actually answered this question but regardless of the pedantry of whether it is a non-existant mouseevent or a MotionEvent #Satyam asked how to get the name of the action not its value!! Instead of criticizing the questioner, or highlighting his knowledge gap, for not reading. it behoves the answerers to actually read the question. This should be used:
MotionEvent ev
me.actionToString (me.getAction());
It is very useful when logging an app's progress while debugging. I wish there was a similar method for the DragEvent but I have to take a longer path...
You can put code like,
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
//code here....
break;
case MotionEvent.ACTION_UP:
//code here....
break;
case MotionEvent.ACTION_MOVE:
//code here....
break;
case MotionEvent.ACTION_CANCEL:
//code here....
break;
}
Checkout this link
http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_CANCEL
MotionEvent class have all listed event for finger touch
There's no such thing as mouse event on Android. You probably meant MotionEvents. You have the description and explanation of each action in the official SDK documentation (which btw you should read before asking).
`
private static String getActionName(MotionEvent event) {
try {
for (Field f : MotionEvent.class.getFields()) {
if (f.getName().startsWith("ACTION_") &&
f.getInt(null) == event.getAction()) {
return f.getName();
}
}
}
catch (IllegalAccessException ignored) {}
return event.getAction()+"";
}
`
Related
I would like to set image1 for the button, but when it is pressed - change it to image2. After releasing, the button should be again an image1. I tried to do it this way in onClick() method:
button.setImageResource(R.drawable.image1);
if(button.isPressed())
button.setImageResource(R.drawable.image2);
but after first pressing the image of button changed to the image2 and stayed like that.
Could You help me with that problem?
I think this is what you want:
MyCustomTouchListener myCustomTouchListener = new MyCustomTouchListener();
button.setOnTouchListener(myCustomTouchListener);
Now the MyCustomTouchListener :
class MyCustomTouchListener implement OnTouchListener {
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// touch down code
button.setImageResource(R.drawable.image1);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
button.setImageResource(R.drawable.image1);
break;
}
return true;
}
}
You can do this easily with a state list drawable, and it requires no additional java code on your part (unless you are creating a StateListDrawable at runtime, but even that is a more suitable approach than implementing custom touch interaction).
Use the following:
int dispImg = 0;
button.setImageResource(R.drawable.image1);
if (button.isPress()) {
if (dispImg == 0) {
button.setImageResource(R.drawable.image2);
dispImg = 1;
}
else if (dispImg == 1) {
button.setImageResource(R.drawable.image1);
dispImg = 0;
}
}
Explanation: dispImg keeps track of the image you're showing. When it is 0, it means that the 1st Image is showing and so, we should switch to the 2nd.
Hope I Helped :D
I apologise for the somewhat noobish question.
I've tried googling it, searching this site and the android developer site, yet didn't manage to find an answer.
I'm looking for a listener that differentiates between up and down events. Currently I'm using OnTouchListener, but it gets triggered very fast, causing a noticable lag, even when I immediately return after an event that is not Down and Up.
I've also tried OnClickListener, but it seems to only get triggered when you lift your finger up, rather than have seperate events for down click and up click like I need.
Would appreciate some help,
Thanks!
YourBtn.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL){
//do stuff
}
return true; //return false can be used it depends
}
});
P.S: it can be: a button, a textview, an imageview or any UI element
I strongly recommend to always add ACTION_CANCEL to avoid any misbehaviours or crashes
See this:
https://developer.android.com/training/graphics/opengl/touch.html
Override the following method on your activity, then you can treat each case, when the user touch the screen, and when he take of his finger.
#Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
// do your stuff
break;
case MotionEvent.ACTION_UP:
// do your stuff
break;
}
return true;
}
You should look at GestureDetector to simplify your touch event handling.
When I touch the screen and move my finger I do something (pullanimation1 and 2) and when I release the screen I do something else (fireanimation1 and 2). Sometimes, the user might touch the screen while pullAnimation or fireAnimation is running, I get errors as the animation then run several times. I would like to make sure the animations won't run more then once when the user touch again the screen.
NB: pullAnimation1 and 2, fireAnimation 1 and 2 are AnimationDrawable
Here is what I've done :
image2.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
boolean bool=false;
boolean bool2=true;
int action = arg1.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (bool2) {
startAnimation(pullAnimation1,pullAnimation2);
bool=true;
}
break;
case MotionEvent.ACTION_MOVE:
if (bool2==true){
Log.w("GAMEACTIVITY","move");
bool=true;
bool2=false;
}
break;
case MotionEvent.ACTION_UP:
startAnimation(fireAnimation1,fireAnimation2);
bool=false;
doPhotoTask();
bool2=false;
break;
}
return bool;
}
});
I think you should be able to use the hasStarted() and hasEnded() methods to determine if your animation is currently going. See the docs for more
Some if statement like this might work:
if((fireAnimation1.hasStarted() == false) || (fireAnimation1.hasEnded == true()){
startAnimation(fireAnimation1, fireAnimation2);
}
I imagine you may also need to use reset() after it is done playing in order or the methods to return proper values next time touch happens.
EDIT:
AnimationDrawable has an isRunning() method, which makes it even easier than View animations.
if(fireAnimation1.isRunning() == false){
startAnimation(fireAnimation1, fireAnimation2);
}
I am new to android development, making a simple game and using onKeyDown(.....) function but it works only for 1 key at a time, how to handle 2 keys at a time means can i move right or left with continous firing.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_RIGHT:
bottle_movx+=2;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
bottle_movx-=2;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
fire=bottle_movx;
firechk=true;
break;
}
invalidate();
return super.onKeyDown(keyCode, event);
}
EDIT: Perhaps this could be of some help?
How do I handle simultaneous key presses in Java?
Quote:
One way would be to keep track yourself of what keys are currently down.
When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.
Then in your game loop, you can do actions based on what's in the list of keys.
EDIT 2: I also found this link which could be useful:
How do I handle multiple key presses in a Java Applet?
Please also check the action event( event.getAction() ). This would return the constant defined in the KeyEvent class like ACTION_DOWN, ACTION_UP etc. Please check for either up or down action.
I'm trying to make a small android Jump and Run game but my problem is that i can't configure the event ACTION_UP right. Here my Code:
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("OTE", "down");
touchDownTrue = true;
break;
case MotionEvent.ACTION_UP:
Log.d("OTE", "UP");
touchDownTrue = false;
break;
}
}
the case MotionEvent.ACTION_UP is never called and i don't know why, the same happens if i use ACTION_CANCEL
After I insert return super.onTouchEvent(event); at the end of the method (onTouchEvent must return a value) your code works for me, when I put it in a blank main activity.
You should probably return true instead of breaking in those cases because you are responding to the event.