4

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.

ronash
  • 856
  • 1
  • 9
  • 15
  • are you using a gesture detector? what is your relevant code where you are checking the event.getAction? – jkhouw1 May 12 '11 at 12:05
  • @Override public boolean onTouchEvent(MotionEvent event) { if ( event.getAction() != MotionEvent.ACTION_DOWN) //never invoked Sorry, I don't know how to format in replies... Thank you for your help! – ronash May 12 '11 at 12:11

1 Answers1

13

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

jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • That did the trick - thanks! It was weird because it worked perfectly well in a different application - now I find out the other one returns true, while this one returns super.onTouchEvent. Thanks a lot! – ronash May 12 '11 at 12:25