0

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();
 }
 });
asked Feb 14, 2012 at 7:05

2 Answers 2

1

i think i would just keep a boolean flag to see if the timer is running or not. Something like that (not tested)

button.setOnClickListener(new OnClickListener() {
 private boolean running = false;
 private CountDownTimer timer;
 @Override
 public void onClick(View v) {
 if(!running)
 {
 running = true;
 timer = new CountDownTimer(4000, 1000) {
 @Override
 public void onFinish() {
 button.setText("SENT"); 
 }
 @Override
 public void onTick(long sec) {
 button.setText("CANCEL (" + sec / 1000 + ")");
 }
 }.start();
 }
 else
 {
 timer.cancel();
 button.setText("Press Me");
 running = false;
 }
 }
});
answered Feb 14, 2012 at 9:04
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand the problem correctly: The countdown timer is displaying and when you cancel you want the timer to stop and the button to revert to "PRESS ME"

CountDownTimer has a "feature" whereby it cannot cancel itself from OnTick... I know it's nuts.

Luckily someone has written a replacement drop in class. https://gist.github.com/737759

I use this class in one of my apps. Just create a class in your project called "CountDownTimer". Remove your import for "android.os.CountDownTimer" and it should work.

EDIT: RE ADJUSTMENT OF ANSWER

I have noticed that you have 2 onClick's for the Button.

I recommend the following:

1) Create a Boolean called "isCounting" 2) Set "isCounting" to FALSE in your Activities onCreate method 3) Create the CountDownTimer separately

CountDownTimer cdt;
// setup countdown timer
cdt = new CountDownTimer(intMilli, 1000)
{
 @Override
 public void onFinish() 
 {
 //your code
 }
 @Override
 public void onTick(long millisUntilFinished) 
 { 
 //your code
 }
};

4)In your Button's onClick check "isCounting"

if (isCounting == FALSE)
{
 cdt.start();
 isCounting = TRUE;
}
else
{
 cdt.cancel();
 isCounting = FALSE;
 button.setText("PRESS ME");
}
answered Feb 14, 2012 at 7:33

4 Comments

Ok great. I added this class and it's working properly. But how would I go about reverting the button using this? I'm really struggling to figure it out.
Could you (inside your onClick) button.setText("PRESS ME"); after cancel();
But won't that just change the button's text and not allow it to be pressed again? Meaning that it won't start the countdown over.
Just amended my answer as I noticed you have 2 onClickListeners

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.