The Technology Magazine.

Full width home advertisement

Android

Linux

Post Page Advertisement [Top]

Android Count Down Timer Project

This is a sample application,  we will create a countdown timer in android using the CountDownTimerclass .This is an example of showing a 30 second countdown in a TextView . We will update the TextView on every tick of the timer.The option to stop and restart the timer is also provided.


So let us start:

1.Create a new project File ->New -> Project ->Android ->Android Application Project. While creating the new project, name the activity as TimerActivity(TimerActivity.java) and layout asactivity_timer.xml.

2.Now let us design the UI for the TimerActivity i.e activity_timer.xml with a textview and a button.

activity_timer.xml


01<?xml version="1.0" encoding="utf-8"?>
02<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03android:layout_width="fill_parent"
04android:layout_height="fill_parent"
05android:orientation="vertical"
06android:background="@drawable/bgi" >
07 
08<TextView
09android:id="@+id/timer"
10android:layout_width="wrap_content"
11android:layout_height="wrap_content"
12android:layout_centerHorizontal="true"
13android:layout_centerVertical="true"
14android:paddingRight="10dip"
15android:textSize="50dp" />
16 
17<Button
18android:id="@+id/button"
19android:layout_width="fill_parent"
20android:layout_height="wrap_content"
21android:layout_alignParentBottom="true"
22android:layout_alignParentLeft="true"
23android:text="Start" />
24 
25</RelativeLayout>

3.Now in the TimerActivity we implement the count down timer by extending the CountDownTimer class.Press Ctrl+Shift+O for missing imports after typing the code.

TimerActivity.java

01public class TimerActivity extends Activity implements OnClickListener {
02 
03 private CountDownTimer countDownTimer;
04 private boolean timerHasStarted = false;
05 private Button startB;
06 public TextView text;
07 private final long startTime = 30 * 1000;
08 private final long interval = 1 * 1000;
09 
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12  super.onCreate(savedInstanceState);
13  setContentView(R.layout.activity_timer);
14  startB = (Button) this.findViewById(R.id.button);
15  startB.setOnClickListener(this);
16  text = (TextView) this.findViewById(R.id.timer);
17  countDownTimer = new MyCountDownTimer(startTime, interval);
18  text.setText(text.getText() + String.valueOf(startTime / 1000));
19 }
20 
21 @Override
22 public void onClick(View v) {
23  if (!timerHasStarted) {
24   countDownTimer.start();
25   timerHasStarted = true;
26   startB.setText("STOP");
27  } else {
28   countDownTimer.cancel();
29   timerHasStarted = false;
30   startB.setText("RESTART");
31  }
32 }
33 
34 public class MyCountDownTimer extends CountDownTimer {
35  public MyCountDownTimer(long startTime, long interval) {
36   super(startTime, interval);
37  }
38 
39  @Override
40  public void onFinish() {
41   text.setText("Time's up!");
42  }
43 
44  @Override
45  public void onTick(long millisUntilFinished) {
46   text.setText("" + millisUntilFinished / 1000);
47  }
48 }
49 
50}
4.Run the project by rightclicking project Run as → android project.

Output:

The output of this example would be similar to the one as follows:

 

No comments:

Post a Comment

Dear visitor,
Please do not post spam. All comments will be moderated before approval.

Bottom Ad [Post Page]

| Designed by Techeia.com