Learn About CountDownTimer in Android Using Android Studio

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.    
  8.     <TextView  
  9.         android:id="@+id/textView"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_centerVertical="true"  
  14.         android:paddingRight="10dip"  
  15.         android:textSize="50dp" />  
  16.    
  17.     <Button  
  18.         android:id="@+id/button"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_alignParentLeft="true"  
  23.         android:text="Start" />  
  24.    
  25. </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.  }  
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.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.countdown.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </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


Similar Articles