How can I display countdown timer on the canvas in android? - android

I am making a simple android app in which i want to display countdown timer on canvas.
I just want that when the game starts the countdown timer begins with 60 sec and decrements along.....
Pls can someone suggests me how do i use the countdown timer class?

There's plenty of documentation available if you actually look for it:
// 60 second (60000ms) timer with ticks every second (1000ms)
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Now just replace the mTextField.setText() lines to something that would update your Canvas (or update the variable the Canvas uses for your time notification, then invalidate() the Canvas to display the update).
Even though I believe it's self-explanatory, when onFinish() is called, the CountDownTimer has reached 0 and is finished counting down.

Related

Create multiple countdowns in android

I am new to android and i am trying to implement a simple timer.For example i have one button and every time i click this button, a dialog shows up and i can set the time.
This time should then be displayed on the same activity, where the button is.
I am fairly new to android and i have only a button on my main activity.
My Questions now : How can i dynamicly add "countdowns" to my main_activity.Lets say maximum is 3.Is there something like a countdown class already or does TimePicker this for me ?
Below code runs a timer for 30 seconds. If you want the user to choose time, you can use an EditText to get the time from user and put it instead of 30000 in below code.
final CountDownTimer timer;
timer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timerText.setText("seconds remaining: " + String.valueOf(millisUntilFinished / 1000));
}
public void onFinish() {
timerText.setText("done!");
}
};

CountDownTimer skipping one second when displaying on TextView

I want to display a simple CountDownTimer in a TextView, but it seems to go from 30 down to 28 right away, as if there was a lag in between. I'm not sure how to go about on fixing this small bug. Here is my code:
This is in the click listener of the Button:
new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
There are 2 issues:
The first issue is that the countdown timer doesn't execute until time has elapsed for the first tick, in this case, after 1000ms.
The second is that the elapsed time is only approximate. millisUntilFinished is not guaranteed to come in increments of the interval (you can see that if you don't divide by 1000, the first tick is slightly under 29000).
Another thing to keep in mind is that you're not guaranteed to receive a tick call. That is to say, if the device did not have enough time to complete a tick, it may skip it (generally only noticeable for faster intervals).
To solve issue 1, you can simply run the onTick code (or refactor it into its own method) and run it as you start the countdown timer.
For issue 2, you can simply round the number.
For example:
new CountDownTimer(30000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
performTick(millisUntilFinished);
}
#Override
public void onFinish()
{
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
performTick(30000);
void performTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
}
If you want to make sure the appropriate value is updated with minimal latency, you may want to consider reducing the interval.
It is skipping because CountDownTimer is not a precise timer. Try to print just the millisUntilFinished without dividing it you'll see some excess number that it wont be exactly 29000 when it hits its first tick.
you can refer on this thread for the solution.

Android - Properly creating and countdown of timer?

Ive been working hard this week at learning android, and one thing I cant find a good
example for is timers, so if someone could maybe take the time to write asimple timer that counts down and displays in a textview, i would be eternally grateful, thanks a lot!
Following code will start from 30sec and end when it reaches 1 sec. Please note that timer uses milliseconds instead for seconds.
1 sec = 1000 milliseconds
Thus, 30 sec = 30x1000 milliseconds
mTextField is your regular TextView, replace it with your own TextView.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

how can i set the time in digital clock

I want to build an android application, this application allow the users to answer some questions.
For every question, User have to answer it in 360 seconds, so I want to make a digital clock starts from 360secons and then down to 0 seconds
I read that I can't set time on digital clock , is it so?
or I have to use Chronometer?
this is my digital clock
DigitalClock timer;
public void init(){
timer=(DigitalClock)findViewById(R.id.dcTimer);
}
any help appreciated.
Use CountDownTimer instead.
That link also shows you how to update a TextView every 1 second.
Just change new CountDownTimer(30000, 1000) so the first parameter is 360000 instead of 30000 and that will give you a good start to your code.
EDIT : You need to import android.os.CountDownTimer and the code example I referred to is below. Good luck.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

how can i show elapsed time in text view from 1h 30min

i m creating an online examination app.
this app require to show elapsed time from 1h 30min.
what should i take timer or what?
elapsetime = (TextView)findViewById(R.id.txtElapsedTime);
elapsetime.setText(" "+hour+" h "+min+" m");
any suggestions?
Use CountDownTimer.
Use this : Example of showing a 30 second countdown
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
stop the Counter we use the countDownTimer.cancel()
if you want to Pause and Resume the timer refer below link
http://example.javamonday.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm
Refference : http://developer.android.com/reference/android/os/CountDownTimer.html

Categories

Resources