I’m trying to make a regular button that functions like a toggle button, but I’m running into some problems. I am new to Java and OOP, so be gentle.
At first my button will say “Press Me.” When my button is pressed, I want a countdown to appear on the button and while it is counting down, I want to be able to press it again to cancel the countdown and revert to its original state. As of now, I am able to press the button, initiate the countdown and cancel it, but I can’t figure out how to make the button revert back to the starting look “Press Me” (if it were pressed again, the countdown would start over and it could be cancelled again).
I tried using a toggle button but I feel I’m pretty close to getting this to work. Any ideas? Here is the code I’m working with now:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(4000, 1000) {
@Override
public void onFinish() {
button.setText("SENT");
}
@Override
public void onTick(long sec) {
button.setText("CANCEL (" + sec / 1000 + ")");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cancel();
}
});
}
}.start();
}
});
i think i would just keep a
booleanflag to see if the timer is running or not. Something like that (not tested)