In my UI I have a button and a label.
On the button click I want to start a new thread which would update the label text to display the timer information (like 00:00 seconds).
This should be updated every second.
Could someone give a simple solution to this problem please?
Assuming that the information is in the form of a textView (called tv), you would use a countdowntimer:
new CountDownTimer(30000, 1) {
public void onTick(long millisUntilFinished) {
mTextField.setText(/* Whatever you want for each millisecond */);
}
public void onFinish() {
mTextField.setText("00:00");
}
}.start();
the second parameter in the CountDownTimer constructor is the intervals for which onTick, so for this timer, onTick will be called every millisecond.
Any questions?
Related
Hi I'm working on an app with a countdown timer. When the button is clicked the timer starts based on the user inputs into the text field,when I change the value of the text field and a new value for the timer starts, it flashes and shows the previous countdown value and counts both the values down,flashing between the previous and the current countdown value. How do I get the timer to forget the value from before and only use the current timer value.
I tried using the cancel function however that didn't work, I think it's something in my tick function however I'm not sure what it is.
Here is my code:
CountDownTimer mcountDownTimer = new CountDownTimer(getmonTime(), 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
long hr1=TimeUnit.MILLISECONDS.toHours( millisUntilFinished);
long sub1=hr1*60;
long min1=TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
long sub2=min1*60;
timer.setText(""+String.format("%d hr,%d min, %d sec",
TimeUnit.MILLISECONDS.toHours( millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)-sub1,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-sub2,
-
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
timer.setText("0");
}
};
mcountDownTimer.start();
}
};
You probably don't cancel the previous one.
Save the instance of the CountDownTimer mcountDownTimer (BTW rename it to mCountDownTimer)
And before setting a new one, cancel the old one using:
mCountDownTimer.cancel();
and only then create the new instance.
E.g:
CountDownTimer mCountDownTimer;
#Override
public void onCreate...
...
if (mCountDownTimer != null) mCountDownTimer.cancel();
mCountDownTimer = new CountDownTimer...
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 would like to add a TextView to my layout that holds an integer value and continues to change it's value for some length of time. For example, the TextView could change its value for 10 seconds and then stops. How can I do this?
You may want to look into a CountdownTimer. The code would look something like this:
CountDownTimer myCountDown = new CountDownTimer(10000, 1000){
public void onTick(long millisUntilFinished) {
myTextView.SetText(String.valueOf(millisUntilFinished / 10));
}
public void onFinish() {
myTextView.SetText("Done!");
}
}.start();
This will create a timer that runs for 10 seconds and updates the TextView every second. an important thing to note is that the parameters are in milliseconds, not seconds. The first parameter (10,000 in my example) represents the duration of the timer. The second parameter (1,000) determines how many milliseconds occur between each call to onTick().
I'm making an application and a certain part in my application I store a list of prices along with the time that the prices last for (they do not all last the same amount of time). So a price lasts a certain time then once that price's time is up it changes to another price (this part changes the UI or basically it updates a textview with the new price). So what I need is a timer that sets the timer again with the new time length and once it's done make the UI change. For instance say that each of the pairs represent the price amount and the time (in seconds): { {$2.53,1.4s}, {$4.57,4.45s}, {$1.23,3.6s}...}
So when the timer starts off the textview displays $2.53 and the timer lasts 1.4s and then it should grab the next price $4.57 and be set again but this time for 4.45s. This process continues on and on until the game is finished. I was thinking of using the CountDownTimer and resetting itself once the onFinish() method is called (I haven't verified if this idea works yet). Are there any other ideas?
You can use a countdown timer and onFinish method you call back the function and it starts another timer:
private void startWheatPrices(long gameTime)
{
//other stuff executed
StartWheatTimer(GameTimeDifference);//starts the timer for the first time
}
private void StartWheatTimer(long TimerAmount)
{
WheatTimer = new CountDownTimer(TimerAmount, 10) {
public void onTick(long millisUntilFinished)
{
}
public void onFinish()
{
//other stuff executed
WheatPricesTV.setText(Float.toString(PriceList.get(0).get(WheatPriceIndex).price));//price is changed
if(InGameplayMode)
StartWheatTimer(convertToMilliseconds(PriceList.get(0).get(WheatPriceIndex).timeLength));//call back the function to start the timer again
}
}.start();
}
I have Separated CountDownTimer in every activity in my app, but the problems happen when I switch between the activities , there is delay some milliseconds and I need the switching between the activities becomes directly without any delay. for instance , I'm in the activity 1 which has CountDownTimer , when I press the button to go to the second activity that has CountDownTimer also , it takes some milliseconds to bring the activity 2. and this is my code
countDownTimer21 = new CountDownTimer(6000, 1000) {
public void onTick(long millisUntilFinished) {
strLong = Long.toString(millisUntilFinished / 1000);
time.setText(strLong);
}
public void onFinish() {
Intent fail = new Intent(Test10_D.this, FailPage10.class);
fail.putExtra("scorerecord", myscore);
next.putExtra("scorevalue", strLong);
startActivity(fail);
countDownTimer21.cancel();
//finish();
}
}.start();
Any suggestions? Thanks Alot
I think You have created two countdown timers in two different activity and when you go from 1st->2nd activity first timer is stooped and 2nd is resumed from value provided by 1st activity's timer.
So Solution is write single common timer in a separate thread like this:
Write your Countdown Timer in a separate service and show the countdown on UI through handler of activity which is on screen.
that's it