How to Make a Countdown Timer in Android?

This Android Program lets you create a Count Down Timer Application.

Here is source code of the Program to create a Count Down Timer Application using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

 
package com.example.contdowntimer;
 
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity implements OnClickListener {
 
    private santimer timer;
    private Boolean timer_flag=false;
    private final long startTime = 50000;
    private final long interval = 1000;
    private Button start;
    private TextView text,time_eplapsed;
    private long timeElapsed;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button) findViewById(R.id.startbutton);
        start.setOnClickListener(this);
        text = (TextView) findViewById(R.id.timer);
        time_eplapsed = (TextView) findViewById(R.id.timeElapsed);
        timer = new santimer(startTime, interval);
        text.setText(text.getText() + String.valueOf(startTime));
    }
 
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (!timer_flag) {
            timer.start();
            timer_flag = true;
            start.setText("Start");
        } else {
            timer.cancel();
            timer_flag = false;
            start.setText("Reset");
        }
    }
 
    private class santimer extends CountDownTimer {
 
        public santimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
 
        }
 
        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            text.setText("Time's up!");
            time_eplapsed.setText("Time Elapsed: " + String.valueOf(startTime));
        }
 
        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            text.setText("Time remain:" + millisUntilFinished);
            timeElapsed = startTime - millisUntilFinished;
            time_eplapsed.setText("Time Elapsed: "
                    + String.valueOf(timeElapsed));
        }
 
    }
}

MainXml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/startbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start" 
        android:textAlignment="center"
       >
    </Button>
 
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp" >
 
        <TableRow>
 
            <TextView
                android:id="@+id/timer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:text="Time: "
                android:textSize="20dp" />
        </TableRow>
    </TableLayout>
 
    <TextView
        android:id="@+id/timeElapsed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Time elapsed: "
        android:textSize="20dp" />
 
</LinearLayout>

Capture

advertisement
advertisement

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.