Game timer countdowns
deepak - Mon Dec 19 2011 13:41:49 GMT-0500 (EST)
Creating a timer countdown for Android?
Creating a timer countdown for Android?
deepak - Mon Dec 19 2011 13:42:34 GMT-0500 (EST)
CountDownTimer SDK reference
deepak - Mon Dec 19 2011 13:43:11 GMT-0500 (EST)
Sample code
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: "
+ millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
deepak - Mon Dec 19 2011 13:44:12 GMT-0500 (EST)
Another Sample code
package com.android.countdown;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
public class CountDownTest extends Activity {
TextView tv; //textview to display the countdown
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
//5000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000,1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText(?done!?);
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText(?Left: ? + millisUntilFinished/1000);
}
}
}
deepak - Mon Dec 19 2011 13:48:23 GMT-0500 (EST)
Above one displays only in Seconds? I want the timer to be displayed in Minutes:Seconds
Above one displays only in Seconds? I want the timer to be displayed in Minutes:Seconds
deepak - Mon Dec 19 2011 13:53:55 GMT-0500 (EST)
Well its easy, when you set the text to the TextView, format the Time in any fashion you want
//Below code will format the time in Minutes:Seconds format
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String sec = String.valueOf(seconds);
String min = String.valueOf(minutes);
if (seconds < 10)
sec = "0" + seconds;
if (minutes < 10)
min= "0" + minutes;
output = min + " : " + sec;
return output;
}
deepak - Mon Dec 19 2011 14:25:39 GMT-0500 (EST)
How to do a Count up Timer?
How to do a Count up Timer?
deepak - Mon Jan 16 2012 22:45:10 GMT-0500 (EST)
Here is how you do a Count up timer