I want to make a android timer app. The timer starts when we press a button and hold it and stops when releasing a button. Please tell me the method that functions the same. I am following an example http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/ to make a timer.
override onTouch listener to the button with your timer logic
findViewById(R.id.btn_add).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// start your timer
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// stop your timer.
}
return false;
}
});
I think this could help you:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// Button is pressed
break;
}
case MotionEvent.ACTION_UP: {
// Button is not pressed
}
}
return true;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With