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();
Related
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!");
}
};
I have 3 buttons.
If I click on a button, I have to disable all the buttons until the countdown timer expires.
I am trying to make a Global countdown timer.
Could you tell how to make it working?
I haven't tried this myself, but some quick searching found a CountDownTimer class in Android that looks like it might work for you.
The example code they give looks pretty straightforward:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
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.
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();
Are there any sample stopwatch project ( or tutorial ) that stopwatch timer will be shown in notificationbar and will keep working even if application closed ?
A simple countdown timer will work, and will persist through onPause() if you allow it to.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Refer to this source for more information. You can also update TextViews if you need to show the timer.
Edit for count up specifically, try Chronometer, which can be found at this link. You can also find a demo of this in the Samples APK.