I'm trying to create a countdown timer for my app but after doing this, the TextView where it should show the remaining time doesn't show anything so my timer is not working. I don't know if I am missed something.
Here is my timer:
setContentView(R.layout.layout7);
cronometro = (TextView) findViewById(R.id.cronometro);
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
cronometro.setText(Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
cronometro.setText("done!");
}
}.start();
Edit: As Simon said and changing some things the timer works now. Is there a way of setting the format into MM:SS
cronometro.setText(DateUtils.formatElapsedTime(millisUntilFinished/1000));
Related
I have a problem with countdown timer.I try some of solutions and articles in this site but they never worked for me. so, please read my codes...
also I used
handler.postDelayed(new Runnable() {
before and it was not my solution but it just worked correctly.
Main question is:
I want to do something like below:
(button pressed)
do some codes1
delay1
do other codes2
delay2
go back to *do some codes1* again.
In short, this is my real code:
itimesec--;
setdelay();
irepeat--;
setrelax();
and this is in my functions:
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
}.start();
yourCountDownTimer1.cancel();
}
I tried to use a variable insted of 50000 but it was not useful anyway.
I tried to put setrelax funtion codes directly into oncreate but it never worked.
it just jumped to
}.start();
yourCountDownTimer1.cancel();
every times and go out.
I tried all the codes without any delay function and they runned correctly.
what is my wrong please...
You need to remember that your code won't be executed sequentially when using the CountDownTimer because it's working asynchronously (via Handler).
Let's dissect your code. Your following code here:
public void setrelax(){
// 1. Creating CountDownTimer
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
// 2. onTick called
...
}
public void onFinish() {
// 3. onFinish called
...
}
}.start();
// 4. CountDownTimer is cancelled.
yourCountDownTimer1.cancel();
}
will be running in the following sequences:
Creating CountDownTimer
CountDownTimer is cancelled.
onTick called 49 times repeatedly (50000/1000 = 50 - 1 ).
onFinish called
So, change your algorithm to something like this:
do some codes1
delay1
--> When delay finished, do other codes2. Then do delay2
You need to call the next code in the CountDownTimer.onFinish()
I looked at your code and I could not find any big mistakes but try this instead
Instead of
.start();
use
yourCountDownTimer1.start();
and remove yourCountTimer1.cancel();
like this :
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
};yourCountDownTimer1.start();
}
Hope it helps.
below is the code for running otp timer in our code.you can copy paste i have mentioned the comments please follow the same.
CountDownTimer countDownTimer; //define countDownTimer
countDownTimer.start(); // start countdown timer on some event like on click
String x="Your code will expire In";
String y="mins";
counttimer(); call the method counttimer which includes all the code of timer
public void counttimer(){
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
String text = String.format(Locale.getDefault(), x+" %02d : %02d "+y,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
phonever_timer.setText(text); //set timer into textview object
}
public void onFinish() {
phonever_timer.setText("Otp Expired..!");
}
};
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();
In my application, i have 1 imageview. I want to pause the countdowntimer for 10 seconds when and resume "automatically" after 10secs. How can i do that? Any suggestions would be highly appreciated.
Note: I am a newbie in android programming.
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
//your code goes here
}
}, delaytime,timeperiodtorun);
Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
Example of showing a 10 second countdown in a text field:Source
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start()
Im back to ask another question :)
In an app I am making, I need a selector to choose one of 4 different countdown timers, for example round1, round2, etc.
but i have 2 problems with the same source. Using the default countdowntimer implentation which doesnt involve naming it round1 but running as a straight countdowntimer, I can cancel it in onPause using
coutdowntimer.cancel();
but after naming it round1 I can no longer pause it and the round1.cancel(); comes up as undefined.
I have spent most of yesterday and throught to 3am trying to find a solution and i thought i had it by making my timers final but it still didnt work.
after thinking about it more i relaised once ive solved this problem, I'm going to have to implement an if statement in my onpause which i would rather not do in an effort to keep my code clean.
So is there a way to implement a universal cancel command to cancel the root countdowntimer function regardless of the actual name its running under.
for example
public void canceltimers(){
//if round1,2,3,4 is runnng, cancel the main countdowntimer thread altogether.
countdowntimer.Cancel();
}
any help is appreciated and here is my current timer code.
CountDownTimer round1 = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
CountDownTimer round2 = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
// round1.start();
ive not implemented my if statements for the level select yet but will be similar to this
if (level == 1){
round1.start();}
if (level == 2){
round2.start();}
So far i have tried setting it as final, with no joy.
I have tried
round1.cancel();
with no joy. as well every implementation of countdowntimer i can think of or find.
Now as a request, im not as amazing as making sense of code as most of you guys so if you could try and give an example of the implementation it would be appreciated, Thanks for looking and thanks for your time & help
phil
make a single timer. Add multiple timertasks to this timer. Define the COUNTDOWN_TIME.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
#Override
public void run() {
//execute code when the COUNTDOWN_TIME has ran out.
}
}, COUNTDOWN_TIME);
}
//cancels all sceduled/running TimerTasks
mTimer.cancel();
My objective is to display a simple timer clock on my screen where it starts from 20 and goes on decreasing and when it becomes 0 I want to start an action.
How to achieve this ?
Thanks in advance
I don't know how you want to display it but you can do this also just by taking a TextView and keep changing its Text every second.
static int i = 20;
new CountdownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
i = i-1;
mTextField.setText(String.valuOf(i));
}
public void onFinish() {
//do your stuff here
}
}.start();