Understanding Advanced Aspects of Chronometer

Posted By : Karan Jeet Singh | 08-Jul-2019

Introduction

Most of us who browse shopping apps often must have seen a section of sales or offers to get readily available to users in a certain amount of time. Ever wondered what could be the logic behind its working? Lucky for Android developers, that time management can be easily handled using an in-built widget class called Chronometer.

 

Problem

Now assuming we have tens of thousands of users, maintaining its state can be a real pickle. Every time, the activity gets created, the initialization code of the timer is called and the chronometer is bound to lose its state. So, how do we maintain a state of the chronometer at a certain time where it was last left. Needless to say, the time passed should be provided by the backend of the application, calculated right when the user hits the API call to fetch the time passed along with its other details.

Now, another limitation of a chronometer is that it's not really customizable by calling a separate function. It has to be done using a listener. The default timestamp format of a Chronometer is HH:MM: SS.

 

Solution

This blog also assumes that you are familiar with basic chronometer functions such as start(), stop(), pause(), etc. Thankfully, chronometer accepts its time base in milliseconds. And we usually pass that parameter as:

chronometer.setBase(SystemClock.elapsedRealtime());

We often use Stopwatch or Timer in our phones. Well, chronometer can act as both.

 

For downward counting or making the chronometer act as a timer, we can set the base of the chronometer like this:

 

chronometer.setCountDown(true);
chronometer.setBase(SystemClock.elapsedRealtime()+(60*60*24*1000));
chronometer.start(); 

Now, the chronometer will start counting downwards from 1 day one to 00:00:00.

 

And for upwards counting or, making it act as stopwatch we make use of this:

 

chronometer.setBase(SystemClock.elapsedRealtime() - (60*60*24*1000));
chronometer.start(); 

Unlike, the example above, the chronometer will start counting upwards from day one to infinity or till the context of the activity is preserved.

 

Another problem that stays at hand is that a lot of developers want to display the number of days passed/left in a chronometer. Thankfully, chronometer has a very subtle listener that can calculate the number of days or time as per your specification. Let's take a look:

 

@Override
    public void onChronometerTick(Chronometer chronometer) {
            long time = (SystemClock.elapsedRealtime() - chronometer.getBase())/1000;
            int d   = (int)(time /86400);
            int h   = (int)((time % 86400)/3600);
            int m = (int)((time % 3600)/60);
            int s= (int)((time %3600)%60);
            String hh = h < 10 ? "0"+h: h+"";
            String mm = m < 10 ? "0"+m: m+"";
            String ss = s < 10 ? "0"+s: s+"";
            chronometer.setText(d+"D:"+hh+"H:"+mm+"M:"+ss+"s");
    }

You have successfully learned how to customize a chronometer according to your needs.

 

Conclusions

 

  • A chronometer is a perfectly customizable widget that fulfills all your needs to display time for days.

  • Chronometer loses its context in case of high memory requirements.

References

 

 

About Author

Author Image
Karan Jeet Singh

Karan Jeet Singh's main area of interest is Android Development. He is familiar with Kotlin and C# in other programming languages. He is familiar with tools such as Android Studio, Visual Studio, NetBeans, Eclipse IDE.

Request for Proposal

Name is required

Comment is required

Sending message..