0

How do I set a timer on all of the checkboxes at the same time if I were to uncheck them all at once? If I uncheck "checkbox1" and set up a timer for that checkbox, wouldn't unchecking "checkbox2" override that timer?

  • I have 3 checkboxes and 1 textview,
  • All checkboxes in the state of checked.
  • I want to create a timer when any of them is unchecked.
  • I wish to have a independent timer for each checkbox

For example, when "checkbox1" is unchecked, I wish to create a timer that lasts for 5 minutes and when it only has 1 minute left, I want it to change the textview's text to "you have 1 minute left" as a reminder that the timer is ending soon.

public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    if (!buttonView.isChecked()) {
        new CountDownTimer(300000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                if (millisUntilFinished < 60000) {
                    map.get(buttonView).setText("1 min left!");
                } else if (millisUntilFinished < 30000) {
                    map.get(buttonView).setText("30 sec left!");
                } else {
                    map.get(buttonView).setText(millisUntilFinished/60000 + ":" + (millisUntilFinished%60000)/1000);
                }
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                map.get(buttonView).setText("Spawned!");
            }

        }.start();
    } else if (buttonView.isChecked()) {
        // cancel the timer basically
    }
}

Unlike what i originally wanted to do, i've created a textview for each checkbox, so it wouldn't be confusing to the user. I've created a map which contains (Checkbox, TextView). How do i cancel the timer when the checkbox is unchecked? do i make an arraylist? if so, where would i add the timer into the arraylist?

rx24race
  • 77
  • 8
  • I think using the same textview to display state of timers for three individual checkboxes will be very confusing for the user. I suggest you review your functional requirements. Either have 1 checkbox or have 3 textviews. If, however, you do want to override the previous timer when you uncheck the second checkbox, then your existing solution should be fine. – Aleks G Jul 02 '13 at 13:58
  • You have multiple checkboxes but one textbox. How to you wish to handle: *textview's text to "you have 1 minute left"* for each change in checkbox? – Mark Schultheiss Jul 02 '13 at 15:49

3 Answers3

0

There are all manner of timers and such available to Java / Android. Just looking at the 'related' topics here turns up:

this
this
and so on...

If you want to have multiple timers for your UI you could create a class with the timer and kick off multiple instances - one for each check box event. Keep an array of these objects in your UI class.

You could also have a single timer for the first event and store the deltas for any subsequent check box events.

Community
  • 1
  • 1
ethrbunny
  • 10,379
  • 9
  • 69
  • 131
0

http://developer.android.com/reference/java/util/Timer.html

Create three timers and simply start them with 5 minutes each when a checkbox is false or unchecked or whatever the property is. Use the schedule method of each timer to do that 1 minute left thing you're talking about.

Chucky
  • 1,701
  • 7
  • 28
  • 62
0

My main concern is...how do i set a timer on all of them at the same time if i were to uncheck them all at once?

You can use CountdownTimer. Set an onCheckedChangeListener on your CheckBoxesand create a new instance of your CountdownTimer each time.

if i uncheck "checkbox1" and set up a timer for that checkbox, wouldn't unchecking "checkbox2" override that timer?

Not if you create separate instances of the CountdownTimer for each one it shouldn't give you a problem.

You could store these instances of CountdownTimer in an Array if you needed to so you could iterate over them and cancel and restart each one if you needed.

codeMagic
  • 44,549
  • 13
  • 77
  • 93