Introduction

This article explains about CountDownTimer in Android using Android Studio.
In this application, you can count the time after clicking on the button. You can set the time depending on your needs by changing values in this code. In this first you will use two variables, one is for the start time and another is for the interval. The start time is the start time from value, in this, I have started the timer at 100. An Interval variable is used for how much time the time will be decreased. Create the id of a button, textview and create a class CountDownActivty that extends CountDownTimer. The CountDownTimer class provides onFinish() and onTick() methods.
Step 1
Create a project like this:
Select "File" -> "New Project" -> "Android Application".
Clipboard002.jpg
Step 2
Create an XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="#ff998765" >
  7. <TextView
  8. android:id="@+id/textView"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_centerHorizontal="true"
  12. android:layout_centerVertical="true"
  13. android:paddingRight="10dip"
  14. android:textSize="50dp" />
  15. <Button
  16. android:id="@+id/button"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentBottom="true"
  20. android:layout_alignParentLeft="true"
  21. android:text="Start" />
  22. </RelativeLayout>
Step 3
Create a Java file and write this:
  1. package com.countdown;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. public class MainActivity extends Activity implements View.OnClickListener
  9. {
  10. private CountDownTimer countDownTimer;
  11. private boolean timerStarted = false;
  12. private Button buttonStart;
  13. public TextView textView;
  14. private final long startTime = 100 * 1000;
  15. private final long interval = 1 * 1000;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState)
  18. {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. buttonStart = (Button) this.findViewById(R.id.button);
  22. buttonStart.setOnClickListener(this);
  23. textView = (TextView) this.findViewById(R.id.textView);
  24. countDownTimer = new CountDownTimerActivity(startTime, interval);
  25. textView.setText(textView.getText() + String.valueOf(startTime / 1000));
  26. }
  27. @Override
  28. public void onClick(View v)
  29. {
  30. if (!timerStarted)
  31. {
  32. countDownTimer.start();
  33. timerStarted = true;
  34. buttonStart.setText("STOP");
  35. }
  36. else
  37. {
  38. countDownTimer.cancel();
  39. timerStarted = false;
  40. buttonStart.setText("RESTART");
  41. }
  42. }
  43. public class CountDownTimerActivity extends CountDownTimer
  44. {
  45. public CountDownTimerActivity(long startTime, long interval)
  46. {
  47. super(startTime, interval);
  48. }
  49. @Override
  50. public void onFinish()
  51. {
  52. textView.setText("Time's up!");
  53. }
  54. @Override
  55. public void onTick(long millisUntilFinished)
  56. {
  57. textView.setText("" + millisUntilFinished / 1000);
  58. }
  59. }
  60. }
Step 4
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.countdown"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.countdown.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
When you click on the button to start.
Image
Clipboard02.jpg
When you stop the watch.
Clipboard003.jpg
When all the time has been completed.
2.jpg