motionEvents didn't work Android Java - java

i can't get message in Logcat from events : MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP. I couldn't find solution for it so i ask it here.
I have this overrided method in my class:
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
Log.d("action","Action was MOVE");
break;
case MotionEvent.ACTION_DOWN:
Log.d("action","Action was DOWN");
break;
case MotionEvent.ACTION_UP:
Log.d("action","Action was UP");
break;
default:
break;
}
}
return super.onTouchEvent(event);
}
And in LogCat i can see just message from
MotionEvent.ACTION_DOWN : "action" "Action was DOWN" . No messages from other actions.
I try it in my phone and in AVD.
Thanks for any response.
Answer:
return must be set to true and not for super constructor.

Just Set return to True and not for super constructor :)

Related

Android watch is not detecting touchevents on GridViewPager

I tried to implement a DismissOverlay for my Android Watch application, but it didn't work. So I tried to tear the problem down and implemented the most basic thing related to touch gestures I was aware about :
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case (MotionEvent.ACTION_DOWN):
Log.d(DEBUG_TAG, "Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE):
Log.d(DEBUG_TAG, "Action was MOVE");
return true;
case (MotionEvent.ACTION_UP):
Log.d(DEBUG_TAG, "Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL):
Log.d(DEBUG_TAG, "Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE):
Log.d(DEBUG_TAG, "Movement occurred outside bounds " +
"of current screen element");
return true;
default:
return super.onTouchEvent(event);
}
}
But this dooesn't recognize anything.
So I'm curious if there is something simple I did wrong, or some special stuff related to the watch implementation I have to take care about?
The problem in my case was, that I had a GridViewPager and I tried to get the touchEvents directly from that. But you actually have to implement the touch stuff for the GridViewPagerItem.

Detect when the touch event is over in Android

How can I detect when the touch event is over in Android,in Webviews specifically.
pseudocode:
if (touch event is over) then
do something
end
Thanks!
The MotionEvent object passed to the onTouchEvent(...) method has a getAction() method, you can use it to determine if the event is over. eg:
webViews.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//pointer down.
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
//event has finished, pointer is up or event was canceled or the pointer is outside of the view's bounds
break;
}
return false;
}
}
Also, if you want to consume the event (stop it from propagating) just make the onTouch(...) method return true instead of false.

Java Android : onTouchListener release?

Java/android noob here, I have a problem where using onTouchListener I change the variable x to x+1 and that works fine, but when I release I would like the x variable to decrease in value until it hits zero, like so:
Imageview image = (ImageView) findViewById(R.id.fgView);
image.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
mode=1;
break;
case MotionEvent.ACTION_UP:
mode=2;
break;
case MotionEvent.ACTION_POINTER_UP:
mode2=2;
break;
case MotionEvent.ACTION_MOVE:
mode=1;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode2=1;
break;
}
}
if(mode==1){
x++;
}
if(mode==2){
x * 0.9
}
//mode2 is filler
}
Now that works fine, my problem comes when I want my x to continue to decrease after release, but since it's under setOnTouchListener then it doesn't work.
Maybe something like this:
public boolean onTouchFalse(View v){
if(x>0){
x * 0.9
}
}
or even something like
public boolean image.isOnScreen{
if(x>0){
x = x * 0.9
}
}
tl;dr: I'm looking for some kind of listener that I could use to depreciate my x.
Is there a listener or some class i'm missing? Thanks for any and all help!
final Handler handler = new Handler();
image.post( new Runnable() {
#Override
public void run() {
x--;
handler.postDelayed(this, 100);
}
});
** Note that the 100 is how long in between each subtraction (in millisecs)
There are no touch events if you're not touching the screen. Events need a trigger, maybe a timer could help.

How to manage back button with multiple screens in Libgdx?

If there is some way how to manage back button in Libgdx?
for example in Andengine I have implemented this like that:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
start(MENU);
break;
...
...
}
}
}
I don't have idea how to do it here, because ApplicationListener has only create, show, render...
I tryed this:
if (Gdx.input.isButtonPressed(Keys.BACK)){
new ScreenChangeTask(MyScreen.SPLASH);
}
but it still closes my application.
FYI: I have class Controller extends Game and I use public void setScreen (Screen screen) to switch between screens.
In order to do this properly you need to tell LibGDX to catch the back key:
Gdx.input.setCatchBackKey(true);
You should do this somewhere early in the application. And set it to false when you want the user to be able to use the back key.
set
Gdx.input.setCatchBackKey(true);
then implement below code on keyUp...
#Override
public boolean keyUp(int keycode) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
game.setScreen(new MenuScreen(game)); //MenuScreen is your class Screen
break;
return false;
}
}
}
Hope this will help you

issue with if/else and switch statements

the following code is to handle ACTION_DOWN and ACTION_UP events for a button named clash. the idea is that once if/else determines that the onTouch event was caused by clash and the switch statement then determines what to do based on the action. i don't know if the problem is that the switch statements are not returning true and that may be related to the problem. when i add a return, eclipse says that the code is unreachable which i don't understand. i was under the impression that you can't break out of a switch without break.
what's actually happening is that the first sound will loop but the code never seems to detect the action up when the button is released and so the sound plays forever. any help would be appreciated.
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});
//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> });
Just a thought.

Categories

Resources