Count Down Timer in Android - Androhub

CountDownTimer Banner

Count Down Timer in Android

Today we are going to create a CountDownTimer in Android. CountDownTimer is used to Schedule a countdown until a time in the future, with regular notifications on intervals along the way. For this we are going to use inbuilt CountDownTimer abstract class.

Basically CountDownTimer is working on Seconds but we can manipulate it to work on Minutes, Hours or Days. So in this tutorial also we are going to convert seconds into Hour, Minutes and Seconds.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open strings.xml located under res=>values folder and add following strings. These strings are going to be used in this example.

3. Now create a layout naming activity_main.xml and add the following code. In this there are two button for start/stop and reset timer, an EditText for entering no of minutes to run timer and a TextView for running timer.

4. The below method startTimer(int noOfMinutes) is going to be used for countdown timer. There are  three methods in CountDownTimer class:

  • onTick(long millisUntilFinished) – When a count down start this method will run and we can display the Tick over TextView.
  • onFinish() – When CountDown finished then this method called.
  • onCancel() – This method is used for cancelling CountDownTimer.

5. Finally come to your MainActivity.java and add the following code. The code is very simple to use CountDownTimer. You can implement this code in your project.

6. Finally, run the application and you will get the output as shown in video.

Thanks. :)

 

12 Comments

madhav
Wednesday, October 12th, 2016

It will work well but when i close application it stops and reset from start. so can you help me how can i start timer from left time.

Dr. Droid
Wednesday, October 12th, 2016

HI Madhav,

To start the timer from where you left it, You have to save the last time in SharedPreferences and then when you open your activity again that time read the value from SharedPreference. If value exist start timer from there else start from 0.

Thanks

Nur subhan
Sunday, December 25th, 2016

Hello Dr.Droid

I wanted to create a multi countdown ,.Can you help me,.??
please

😀

Dr. Droid
Monday, December 26th, 2016

Hi Nur Subhan,

To create MultiCountdown there are two ways:
1. Create multiple CountDownTimer instance and use them as to start and stop timer. (I am not sure that this will work out or not)
2. Create a ListView and add the number of rows and in every row put your timer and there you have to start it and stop. (This will work out.)

For 2nd Way you can have look to this StackOverFlow Solution:
http://stackoverflow.com/questions/6346075/android-multiple-simultaneous-count-down-timers-in-a-listview

Thanks

ismail
Monday, December 26th, 2016

Thanks you.
i will use in my project

Nur subhan
Tuesday, January 3rd, 2017

Sory Dr. Droid

I can not understand all of that code
please give me a tutorial from you

:'(

Dilyan
Thursday, May 11th, 2017

Hello,
Can you explain me how to make a resume button in this code.

Thanks in advanced 🙂 !

Dr. Droid
Thursday, May 11th, 2017

Hi Dilyan,

Unfortunately there is no resume functionality in Count Down but there is other way to do this. For this whenever you stop the timer you need to save the time in SharedPreference and when you click on Resume button that time get the remaining time by subtracting total time – save time = time_remaining. This remaining_time will be your current time for which you have to run the count down.
Hope you understand.

Thanks

pppp
Friday, June 2nd, 2017

How to manipulate it for number of days, as i am trying but its not giving the correct value

Dr. Droid
Saturday, June 3rd, 2017

Hi PPPP,

I don’t know what logic you are using. I am telling my logic please cross check with yours one and try it.

1. Convert the milliseconds into hours.
2. After convert hours into number of days by : hours/24.

In this way you will get the countdown timer into number of days.

Thanks

Muzammal Rasheed
Friday, January 11th, 2019

thankew best Tutorial <3

Athul Thomas
Friday, February 22nd, 2019

thank you very much sir….its working

Post comment

Your email address will not be published. Required fields are marked *