user3424455
user3424455

Reputation: 105

Problems with starting and stopping CountDownTimer with buttons

I have an edit text for entering a time in minutes, a button to start a countdown timer using the minutes entered in the edit text which updates a text view, and a button which I want to cancel the countdown timer before it finishes.

The problems I'm having are...the variable the countdown timer needs (the milliseconds) can't get assigned until I click the start timer button because it's coming from an edit text. So I created the countdown timer inside of the onClick for the start timer button. However...I want another button to cancel the timer...and it doesn't have access to the countdown timer. So that didn't solve anything and I'm confused at how to do this. Any advice would help.

Edit: I just realized I could move the stop timer button with the listener to the start timer button onClick also...under the countdown timer. I'm just confused about how the countdown timer could find the milliseconds variable if I have countdown timer outside of the start timer onClick. I guess moving the stop timer under countdown timer works...but still feels like I'm missing something.

Edit2: I tried moving countDownTimer into onCreate and making milliseconds global...but I have to make countDownTimer final in order for the buttons to be able to use it, and countDownTimer just skips to onFinish.

    mTimerTextView = (TextView)findViewById(R.id.timer_text_view);
    mTimerEditText = (EditText)findViewById(R.id.timer_edit_text);

    mStartTimerButton = (Button)findViewById(R.id.btn_start_timer);
    mStartTimerButton.setOnClickListener(new View.OnClickListener() {           
        @Override
        public void onClick(View v) {

            // Getting time in minutes from edit text
            int timeEntered = Integer.parseInt(mTimerEditText.getText().toString());
            long milliseconds = timeEntered * 60000;

            CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    int seconds = (int) (millisUntilFinished / 1000) % 60;
                    int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
                    int hours   = (int) ((millisUntilFinished / (1000*60*60)) % 24);
                    mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));               
                }

                @Override
                public void onFinish() {
                    new AlertDialog.Builder(DailyGoalActivity.this)
                        .setTitle("Time's Up")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {                        
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();                            
                            }
                        }).show();
                }           
            };
            countDownTimer.start();
        }
    });

    mStopTimerButton = (Button)findViewById(R.id.btn_stop_timer);
    mStopTimerButton.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View arg0) {
            // countDownTimer.cancel(); <- How could I do this? 
        }
    });

Upvotes: 0

Views: 1234

Answers (2)

Irshad Khan
Irshad Khan

Reputation: 784

there are two possible reason for your problem first one is that you need to create countdown timer in onCreate and second one is that you are stopping null countdown timer.

solution-1 you can use a boolean variable to check that timer has start or not and then start timer accordingly on button click.Apart from this initialize timer in on create and start in on click follow this code please follow this link

solution-2 to stop the timer use this code

if(countDownTimer != null) {
     countDownTimer .cancel();
     countDownTimer = null;
 }

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Isn't the simple solution to declare CountDownTimer variable globally?

Move this code to onCreate

CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
        int seconds = (int) (millisUntilFinished / 1000) % 60;
        int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
        int hours   = (int) ((millisUntilFinished / (1000*60*60)) % 24);
        mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));               
    }
}

Then start timer in Start button's onClick and cancel in Stop button's onClick.

Hope it helps.

Upvotes: 0

Related Questions