{"id":2297,"date":"2017-03-09T10:59:15","date_gmt":"2017-03-09T10:59:15","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=2297"},"modified":"2019-06-14T10:39:47","modified_gmt":"2019-06-14T10:39:47","slug":"countdown-timer","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/countdown-timer","title":{"rendered":"CountDownTimer Tutorial With Example In Android Studio"},"content":{"rendered":"<p>CountDownTimer in Android is used to\u00a0set a countdown based on interval\u00a0set by you and it will stop when the time has come in future. \u00a0You can use this Count Down Timer for creating any countdown for event.<\/p>\n<p><strong>CountDownTimer Basic Code:<\/strong><\/p>\n<pre>CountDownTimer (long millisecInFuture, \r\n                long countDownInterval)\r\n<\/pre>\n<p>Here <strong>millisecInFuture<\/strong> is the time you set in millisecond when you want\u00a0CountDownTimer to stop and\u00a0<strong>countDownInterval<\/strong> is the interval time in millisecond you set after which number will increment in CountDownTimer.<\/p>\n<hr \/>\n<h4><strong>Methods of CountDownTimer in Android:<\/strong><\/h4>\n<p>Let\u2019s discuss some common methods of a CountDownTimer, which are used to configure a CountDownTimer in our application.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. onTick(long millisUntilFinished )<\/strong><\/span><br \/>\nIt callback fired on regular interval and millisUntilFinished is the number of millis in the future from the call until the countdown is done.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. onFinish()<\/strong><\/span><br \/>\nIt fires then the countdown timer finishes i.e time is up. User can add toast to show that time is up or can set text in textView.<\/p>\n<pre>public  void onFinish()\r\n{\r\n    textView.setText(\"FINISH!!\");\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. start()<\/strong><\/span><br \/>\nIt simply starts the countdown timer.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. cancel()<\/strong><\/span><br \/>\nIt cancels the countdown timer.<\/p>\n<hr \/>\n<h4><strong>CountDownTimer Example in Android Studio:<\/strong><\/h4>\n<p><strong><span style=\"color: #008000;\">Example:<\/span> <\/strong>In the below example of countdown timer we will show you the use of countdown timer in our application. For that we display a textview and a button in our xml file. In java class we used the countdown timer methods and add message when timer is over. Here we had set the time and till that specified time the timer will run and then stop. Below is the final output, download project code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/z3oyzqrd4aneexd\/CountDownTimer.zip\/file\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Download Project Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"> ? <\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2301\" src=\"\/ui\/wp-content\/uploads\/2017\/02\/CountDownTimer-In-Android-Studio.png\" alt=\"CountDownTimer In Android Studio\" width=\"255\" height=\"518\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/02\/CountDownTimer-In-Android-Studio.png 255w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/02\/CountDownTimer-In-Android-Studio-148x300.png 148w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>CountDownTimer<\/strong><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt; <strong>activity_main.xml (or) main.xml<\/strong> and add following code:<\/p>\n<p>In this step we open an xml file and add a button and textview, on clicking button we will call the countdown timer to run and textview will display the time.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/activity_main\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\"com.example.countdowntimer.MainActivity\"&gt;\r\n\r\n    &lt;Button\r\n        android:text=\"@string\/start_timer\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignParentBottom=\"true\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginBottom=\"158dp\"\r\n        android:id=\"@+id\/button\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:textStyle=\"bold|italic\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_above=\"@+id\/button\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginBottom=\"63dp\"\r\n        android:id=\"@+id\/textView\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:textSize=\"40sp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open\u00a0 app -&gt; package -&gt;\u00a0<strong>MainActivity.java<\/strong><br \/>\nIn this step we open MainActivity where we add the code to initiate the countdown timer and a text view to display time and then user can display the message that time is over by using a Toast and also displayed in the textview.<\/p>\n<pre>package com.example.countdowntimer;\r\n\r\nimport android.os.CountDownTimer;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.TextView;\r\n\r\npublic class MainActivity extends AppCompatActivity  {\r\n    public int counter;\r\n    Button button;\r\n    TextView textView;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        button= (Button) findViewById(R.id.button);\r\n        textView= (TextView) findViewById(R.id.textView);\r\n        button.setOnClickListener(new View.OnClickListener()\r\n        {\r\n            @Override\r\n            public void onClick(View v) {\r\n                new CountDownTimer(30000, 1000){\r\n                    public void onTick(long millisUntilFinished){\r\n                        textView.setText(String.valueOf(counter));\r\n                        counter++;\r\n                    }\r\n                    public  void onFinish(){\r\n                       textView.setText(\"FINISH!!\");\r\n                    }\r\n                }.start();\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App in AVD and you will see that on clicking the button will start the timer till the defined time.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Bonus Tip:<\/strong> You<\/span>\u00a0can also run the countdown in decreasing order. For that you just need to set the end value in a variable(counter) and then set it as counter. It will start the countdown from end value.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Recommended Read &#8211;<\/strong><\/span> <strong><a href=\"\/createandroidapp\/create-countdown-timer-app\">How To Create CountDown Timer App In Android Studio: Step By Step Guide<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CountDownTimer in Android is used to\u00a0set a countdown based on interval\u00a0set by you and it will stop when the time has come in future. \u00a0You can use this Count Down Timer for creating any countdown for event. CountDownTimer Basic Code: CountDownTimer (long millisecInFuture, long countDownInterval) Here millisecInFuture is the time you set in millisecond when &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/countdown-timer\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CountDownTimer Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-2297","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>CountDownTimer Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"CountDownTimer in Android is used to set a countdown based on interval set by you and it will stop when the time has come in future. Example in Android Studio explain it step by step.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/countdown-timer\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2297"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2297\/revisions"}],"predecessor-version":[{"id":2844,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2297\/revisions\/2844"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}