Users Online

· Guests Online: 50

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

021 Java Android Program to Demonstrate Count Down Timer Application

This Android Java Program lets you create a Count Down Timer Application.
Here is source code of the Program to create a Count Down Timer Application. 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>

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.02 seconds
10,806,538 unique visits