mkyong
mkyong

Reputation: 12947

Toggle Button in Android App

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();
        }
    });

Upvotes: 0

Views: 986

Answers (2)

Tony
Tony

Reputation: 2425

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");
}

Upvotes: 0

Sprigg
Sprigg

Reputation: 3319

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;
      }
    }
});

Upvotes: 1

Related Questions