Skip to content

Programmatically

Douglas Soares Pereira edited this page Feb 3, 2018 · 6 revisions

To begin, if you prefer you can define the style programmatically:

circularCountdown.setProgressBackgroundColor(R.color.colorAccent)
circularCountdown.setProgressForegroundColor(R.color.colorPrimary)
circularCountdown.setProgressTextColor(R.color.colorPrimaryDark)
circularCountdown.setProgressTextSize(24f)

You have two types of Countdown: Circular Countdown and Circular Cascade Countdown.

Circular Countdown

This is the simple way, just one countdown running.

circularCountdown.create(3, 10, CircularCountdown.TYPE_SECOND).start()

You need to call .create() with three params: pastTime, endTime and timeType and .start() to start the countdown.

  • pastTime is the time that was already passed on the countdown;
  • endTime is the max value of the timer;
  • timeType is the type of the countdown (TYPE_SECOND, TYPE_MINUTE, TYPE_HOUR or TYPE_DAY);

In the example above the Countdown will have 10 positions and start at 7 (because already past 3 of 10). The type define the duration that each position will take, so, every one second one position will be advanced.

You can stop the Countdown anytime you want:

circularCountdown.stop()

You can add a listener too:

circularCountdown.create(3, 10, CircularCountdown.TYPE_SECOND)
                .listener(object : CircularListener {
                    override fun onTick(progress: Int) {

                    }
                    
                    override fun onFinish(newCycle: Boolean, cycleCount: Int) {
                        
                    }
                })
                .start()

In the listener you have onTick and onFinish.

  • onTick is called everytime that your countdown advance one position and it returns the current position;
  • onFinish is called everytime that one cycle is done and returns if have a new cycle or not and the count of cycles that was runned.

You can definy more rules using .maxCyles() and .loop():

circularCountdown.create(3, 10, CircularCountdown.TYPE_SECOND).maxCycles(3).start()

circularCountdown.create(3, 10, CircularCountdown.TYPE_SECOND).loop(false).start()

In the first one the Countdown will run three times and stop and in the second one will run just once. The loop is enabled as default.

You can use other functions to change/check and the names are self explanatory:

circularCountdown.isLoopEnable()
circularCountdown.enableLoop()
circularCountdown.disableLoop()
circularCountdown.getMaxCycles()
circularCountdown.isRunning()

Circular Cascade Countdown

Here you can create a Cascade Countdown connecting two to four Countdowns.

CircularCascadeCountdown(86405000,
                circularCountdownSeconds,
                circularCountdownMinutes,
                circularCountdownHours,
                circularCountdownDays)
                .listener(object : CascadeListener {
                    override fun onFinish() {
                        
                    }
                })
                .start()

You need to call CircularCascadeCountdown() with two to five params: remainTime, countdownSeconds, countdownMinutes, countdownHours and countdownDays and .start() to start the countdown.

  • remainTime is the total time (in miliseconds) that the Countdown will run;
  • countdownSeconds the countdown view referent of seconds (can be styled before);
  • countdownMinutes the countdown view referent of minutes (can be styled before);
  • countdownHours the countdown view referent of hours (can be styled before);
  • countdownDays the countdown view referent of days (can be styled before);

In that case the countdown will run based on seconds.

If you want to manipulate it you can attribute to a variable:

val circularCascadeCountdown = CircularCascadeCountdown(86405000,
                circularCountdownSeconds,
                circularCountdownMinutes,
                circularCountdownHours,
                circularCountdownDays)
                .start()

You can stop the Countdown anytime you want:

circularCascadeCountdown.stop()

And check if is running:

circularCascadeCountdown.isRunning()

Clone this wiki locally