CountDown timer
CountDownTimer is used to Schedule a countdown until a given time, with regular notifications on given time intervals.
CountDownTimer consist of four method shown below:
start() is used to start the CountDownTimer.
cancel() is used to cancel the CountDownTimer.
onTick(long millisUntilFinished) is called in a regular interval
onFinish() is called when the time is up.
The calls to onTick(long millisUntilFinished) are synchronized to this object so that one call to onTick(long millisUntilFinished) won't ever occur before the previous callback is complete.
You can download source code here.
Example of showing a one minute countdown below:
This is code for activity_count_down_timer.xml
CountDownTimer consist of four method shown below:
start() is used to start the CountDownTimer.
cancel() is used to cancel the CountDownTimer.
onTick(long millisUntilFinished) is called in a regular interval
onFinish() is called when the time is up.
The calls to onTick(long millisUntilFinished) are synchronized to this object so that one call to onTick(long millisUntilFinished) won't ever occur before the previous callback is complete.
You can download source code here.
Example of showing a one minute countdown below:
package com.etr.countdowntimerdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class CountDownTimerActivity extends Activity {
Button start;
TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_down_timer);
view = (TextView) findViewById(R.id.textView1);
start = (Button) findViewById(R.id.button1);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
view.setText("seconds remaining: "
+ millisUntilFinished / 1000);
}
public void onFinish() {
view.setText("done!");
}
}.start();
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.count_down_timer, menu);
return true;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class CountDownTimerActivity extends Activity {
Button start;
TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_down_timer);
view = (TextView) findViewById(R.id.textView1);
start = (Button) findViewById(R.id.button1);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
view.setText("seconds remaining: "
+ millisUntilFinished / 1000);
}
public void onFinish() {
view.setText("done!");
}
}.start();
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.count_down_timer, menu);
return true;
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CountDownTimerActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="170dp"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="84dp"
android:text="Start" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CountDownTimerActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="170dp"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="84dp"
android:text="Start" />
</RelativeLayout>
Comments
Post a Comment