How To Show Timer in Android | How To Set Start Time in Chronometer in Android | Android Breakdown

How To Show Timer in Android | How To Set Start Time in Chronometer in Android | Android Breakdown
Timer in android using chronometer.

There are bunch of questions out there and many answers for them. But you may get confused by so many different answers and spending lots of time by applying all of them one by one in your projects. yes of course, It is an time consuming task with a serious headache.

But don't worry, here you will find almost everything you need to know about timer and the answer of this most famous question "How to show timer in Android ?" There are several examples on internet but most of them are not clear to so many developers. That's why we decided to make a good, clean and simple blog on this topic.

So here we will start with chronometer. So basically chronometer is the best option for showing timer because the most main reason is supporting API 1+, which is quite impressive rather than TextClock, which supports API 17+.



Step : 1

So, First of all copy this xml code and paste into you layout. You can customize it by using attributes like textSize, textColor etc.
 



Step : 2

Now, initiate chronometer in your java file (Activity/Fragment).
Chronometer chronometer= (Chronometer) findViewById(R.id.chronometer);

Now chronometer is so simple, it starts tickling from base time and does increment by 1 sec. and one more thing which is important that you can start it by chronometer.start() and stop by using chronometer.stop().

Note : If you start chronometer without giving any base time, it will take the time when you called start() function.


So here are two kind of requirements :
1. Timer should start from 00:00 every time when activity/fragment loads.
2. Timer should be persistence once started.

Now if you want to start from 00:00 every time when activity/fragment loads then use SystemClock.elapsedRealtime() as base time of chronometer.
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();

This one is the best for the requirement number 1. But if we talk about persistence timer with chronometer the logic comes into the play.

So Now we need to save two things here.
1. Start Date Time.
2. Current Date Time


You can also save it on server (because if user changes device time, it won't affect the timer).
Paste the code below in your code to make persistent timer.
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-M-yyyy HH:mm:ss");

        try {
            Date startDateTime = simpleDateFormat.parse([Your Start Date Time]); //Example : 01-01-1990 10:00:00
            Date currentDateTime = simpleDateFormat.parse([Your Current Date Time]); //Example : 01-01-1990 11:00:00

            //milliseconds
            long different = currentDateTime.getTime() - startDateTime.getTime();

            long secondsInMilli = 1000;
            long minutesInMilli = secondsInMilli * 60;
            long hoursInMilli = minutesInMilli * 60;
            long daysInMilli = hoursInMilli * 24;

            long elapsedDays = different / daysInMilli;
            different = different % daysInMilli;

            long elapsedHours = different / hoursInMilli;
            different = different % hoursInMilli;

            long elapsedMinutes = different / minutesInMilli;
            different = different % minutesInMilli;

            long elapsedSeconds = different / secondsInMilli;

            txtDays.setText(elapsedDays > 1 ? elapsedDays + " Days" : elapsedDays + " Day");
            txtDays.setVisibility(elapsedDays > 0 ? View.VISIBLE : View.GONE);


            int elapsedTimeMilliseconds = (int) (elapsedHours * 60 * 60 * 1000
                    + elapsedMinutes * 60 * 1000
                    + elapsedSeconds * 1000);


            chronometer.setBase(SystemClock.elapsedRealtime() - elapsedTimeMilliseconds);
            chronometer.start();


        } catch (ParseException e) {
            Utils.printLog(e.toString());
        }

    }
How To Show Timer in Android | How To Set Start Time in Chronometer in Android | Android Breakdown
Preview of Chronometer


That's all Folks, if you still have any confusion, clear them by commenting below.
And Be Updated with our YouTube Channel.


Comments