{"id":528,"date":"2019-10-16T20:59:17","date_gmt":"2019-10-16T10:59:17","guid":{"rendered":"https:\/\/learntodroid.com\/?p=528"},"modified":"2019-10-16T20:59:27","modified_gmt":"2019-10-16T10:59:27","slug":"how-to-create-a-count-down-timer-in-android","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-create-a-count-down-timer-in-android\/","title":{"rendered":"How to Create a Count Down Timer in Android"},"content":{"rendered":"\n

At the moment I am working on app to help keep me motivated on my goals by using a count down timer to track the time I spend on each of my goals everyday. I have put together this tutorial with what I have learnt on creating a count down timer in an Android app using Java.<\/p>\n\n\n\n

To create an Android app with a count down timer follow the steps below:<\/strong><\/p>\n\n\n\n

  1. Set up a user interface to capture the time in hours, minutes and seconds you wish to count down from<\/strong><\/li>
  2. Set up a user interface to show the time remaining in hours, minutes and seconds before the count down completes with a button to start the timer <\/strong><\/li>
  3. Use a <\/strong>CountDownTimer<\/strong><\/a> to set up the timer that starts when the start button is pressed, providing the time the count will last for and the interval you will update the screen with changes to the count. Override the onTick(long) method to handle screen updates at a set internal and override the onFinish() method to handle the count down expiry<\/strong><\/li>
  4. (Optional) Add additional functionality count down timer to pause, resume and reset the timer <\/strong><\/li><\/ol>\n\n\n\n

    What functionality does the CountDownTimer offer?<\/h2>\n\n\n\n
    • The CountDownTimer was added in Android API level<\/li>
    • It is available in the android.os package and can be imported using “import android.os.CountDownTimer;”<\/li>
    • The constructor for CountDownTimer takes two arguments<\/li>
    • The first argument in the constructor, long millisInFuture, is the number of milliseconds in the future after the count down timer is started (using the start() method) that the timer will expire and trigger the onFinish() callback<\/li>
    • The second argument in the constructor, long countDownInternal, is the interval (in milliseconds) when the onTick(long) callback will be triggered<\/li>
    • The CountDownTimer has a start() method to begin the timer and a cancel() method to end the timer<\/li>
    • When creating a CountDownTimer to use, the onTick(long) and onFinish() methods will be overriden<\/li>
    • Overriding the onTick(long) method will allow you to trigger specific functionality, such as updating the remaining time left in the count down, every time the interval elapses<\/li>
    • Overriding the onFinish() method will allow you to trigger specific functionality once the count down has completed, for example, showing a count down completion message<\/li><\/ul>\n\n\n\n

      Creating an App using CountDownTimer<\/h2>\n\n\n\n

      We will be making a simple count down timer Android app written in Java in this tutorial. The code for this tutorial is available publicly in the Count Down Timer repo<\/a> in GitHub.<\/p>\n\n\n\n

      \"\"
      The Count Down Timer app we will be building<\/figcaption><\/figure>\n\n\n\n

      Step 1: Capturing the Count Down Time<\/h3>\n\n\n\n

      In this Count Down Timer app, we will support the ability to count down from a particular number of hours, minutes and seconds in the future. <\/p>\n\n\n\n

      Add three EditTexts in the user interface to capture the hours, the minutes and the seconds we want to count down from. Put a TextView above the hours, minutes and seconds to describe the units being entered. Between the EditText for hours and minutes and the EditText between minutes and seconds place a TextView with the colon “:” symbol.<\/p>\n\n\n\n

      Below is an excerpt from the layout file for the MainActivity activity_main.xml<\/a> which shows the layout for capturing the time we will be counting down from.<\/p>\n\n\n