Capture button release in Android
You should set an OnTouchListener on your button.
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
}
};
You have to handle MotionEvent.ACTION_CANCEL as well. So the code will be:
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP ||
event.getAction() == MotionEvent.ACTION_CANCEL) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
}
};
use an OnTouchListener or OnKeyListener instead.
You might be able to do this by overriding the onKeyDown
and onKeyUp
. These are both inherited from android.widget.TextView
. Please see the android.widget.Button
doc for (a bit) more info.