8

Is there any kind of onPress and onRelease for android buttons like in flash?

Falmarri
  • 47,727
  • 41
  • 151
  • 191
nebkat
  • 8,445
  • 9
  • 41
  • 60
  • If you implement `KeyboardView.OnKeyboardActionListener`, then you will have access to `onPress()` and `onRelease()` – IgorGanapolsky Jan 27 '16 at 20:23
  • @IgorGanapolsky that's wrong and this is a 5 year old question... – nebkat Jan 27 '16 at 20:59
  • Depending on how you look at it: http://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener.html#onRelease(int). But not really for Buttons per se. – IgorGanapolsky Jan 28 '16 at 01:10

5 Answers5

22

try this:

    someView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            if (arg1.getAction()==MotionEvent.ACTION_DOWN)
                runEnemy(); 
            else
                stopEnemy();
            return true;
        }
    });

arg1.getAction()==0 it's mean ACTION_DOWN http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN

Community
  • 1
  • 1
ihitang
  • 231
  • 2
  • 3
15

It is too late, but maybe someone will find it useful:

mButton.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                    Log.d("Pressed", "Button pressed");
                else if (event.getAction() == MotionEvent.ACTION_UP)

                 Log.d("Released", "Button released");
                // TODO Auto-generated method stub
                return false;
            }
        });
Bobans
  • 439
  • 6
  • 13
8

Yes. Instead of an onClickListener, you have to use an OnTouchListener. http://developer.android.com/reference/android/view/View.OnTouchListener.html

Iota
  • 15
  • 5
Falmarri
  • 47,727
  • 41
  • 151
  • 191
1

You may change mButton, what is yours instead. And you may add paranthesis after if and else if

mButton.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
                Log.d("Pressed", "Button pressed");
            else if (event.getAction() == MotionEvent.ACTION_UP)

             Log.d("Released", "Button released");
            // TODO Auto-generated method stub
            return false;
        }
    });

And this code is from my side:

// butona basınca bir ses çekince bir ses geliyor.
    sescal.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            { 

                Log.d("Pressed", "Button pressed");
                mp.start();
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {

                Log.d("Released", "Button released");
                mp2.start();
            }
            // TODO Auto-generated method stub
            return false;
        }
    });
Manuel
  • 3,828
  • 6
  • 33
  • 48
Bay
  • 467
  • 7
  • 22
1

Take a look at Handling UI Events on the android docs, you specifically want onTouch if you want to listen to down and release. If you are doing this just to change the look of a button there are other ways to handle this in button using state lists.

sgarman
  • 6,152
  • 5
  • 40
  • 44