OnPress / onRelease on Android

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

+6
android
source share
5 answers

Yes. Instead of onClickListener you should use OnTouchListener. http://developer.android.com/reference/android/view/View.OnTouchListener.html

In addition, this has nothing to do with the flash and should not be flagged as such.

+8
source share

try the following:

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 means ACTION_DOWN http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN

+20
source share

Too late, but perhaps 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; } }); 
+13
source share

Take a look at UI Event Handling on Android docs, specifically you want to enable onTouch if you want to listen and release. If you are doing this simply to change the appearance of a button, there are other ways to deal with it in the button using state lists .

+1
source share

You can change the mButton that is in your place. And you can 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 from:

 // 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; } }); 
+1
source share

All Articles