Home Android Tutorial Chronometer Tutorial With Example In Android Studio

Chronometer Tutorial With Example In Android Studio

Chronometer Tutorial With Example In Android Studio
Chronometer Tutorial With Example

In Android, Chronometer is a class that implements a simple timer. Chronometer is a subclass of TextView. This class helps us to add a timer in our app.

You can give Timer start time in the elapsedRealTime() timebase and it start counting from that. If we don’t give base time then it will use the time at which time we call start() method. By default a chronometer displays the current timer value in the form of MM:SS or H:MM:SS. We can set our own format into an arbitrary string.

Chronometer Use Case.

  • Display digital time text with default format like mm:ss. This is commonly use case.
  • Display a count down timer.
  • Display what ever text that you want to display every second.
  • Display different images every second.

By default, chronometer counts upwards. If you want to have countdown chronometer, you need to call setCountDown method passing true as value to it. This feature is useful to count down from future time. Let’s say you want to count down from tomorrow in your app. Then you need to first set setCountDown to true and set base to SystemClock.elapsedRealtime() + one day’s time in milliseconds which is 10006060*24.

Chronometer can be started by calling start method. Before chronometer is started, you need to set starting point of time from that you want chronometer to start the count, by using setBase method and passing starting point of time. By default, meaning if you don’t set base, it starts counting from 0 because Chronometer’s default base time is time at which start method is called.

ChronoMeter is a widget that are used as simple timer. You can give it a start time and it will start counting up from there, or if you don’t provide a base time, it will use the time at which you will call start() method. The timer also counts down from a base time. If you want to count down, you need to call setCountDown(boolean) method with true value.