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();
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 one CountDownTimer and i want to do an action for every two wasted seconds.
countdowntimer = new CountDownTimer(10000, 1) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}.start();
For example when it starts time remaining = 10 seconds
When time remaining = 8 seconds I want to do an action
When time remaining = 6 seconds I want to do an action
and so on......
Just check if it is divisible by 2.
#Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished/1000;
if ((seconds % 2) == 0)
{ // even number--do some action }
}
This is assuming you are calling the onTick() every second like
countdowntimer = new CountDownTimer(10000, 1000) {
This is probably preferable for you
Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often
countdowntimer = new CountDownTimer(10000, 2000) {
then you could just do the action with every call to onTick()
CountDownTimer Docs
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));
what i wish to make is a pie type clock that counts time from 1 minute to 0.
My question is how do i add images or remove them using a script to a layout view and make them rotate at the center of the layout view, depending on a countdown.
What would be the best approach for it? where do i start? and what would i need to read about to make it?
Thanks in advance :]
One way of doing this is that you can create all the images you need and use CountDownTimer to relate to each image.
I do not know what you are exactly trying to do but creating an animation (flash) and putting it into your application may work as long as you have the timing done correctly.
For an analog clock, this is what I use:
public void CDT() {
//CHANGE 31000 TO WHATEVER YOU WANT. FOR 60 SEC, I WOULD USE 60000 OR 61000
new CountDownTimer(31000, 1000) {
public void onTick(long millisUntilFinished) {
//THIS MAKES IT LOOK NICER WHEN IT COUNTS DOWN (eg. :09 instead of :9)
if(millisUntilFinished < 10000) {
textTimer.setText(":0" + millisUntilFinished / 1000);
}
else {
textTimer.setText(":" + millisUntilFinished / 1000);
}
}
public void onFinish() {
textTimer.setText(":00");
new AlertDialog.Builder(SplitEmUpBeta2Activity.this)
.setTitle("TEXT").setMessage("TEXT")
.setNeutralButton("TEXT", null);
.show();
}
}
.start();
}
If you really want to create the "Pie" clock, you need to set onTick to replace the image of the clock with the appropriate one (remove the old and add the new). For example:
public void onTick(long millisUntilFinished) {
if(millisUntilFinished == 60000) {
//REPLACE IMAGE (REMOVE OLD AND REPLACE WITH NEW)
}
// AND SO ON UNTIL YOU FINISH ALL THE IMAGES
}
In my application one option is there which is used to select the application trigger time that is implemented in RadioButtons like : 10 mins, 20Mins, 30mins and 60mins
after selecting one of them the application starts at that time.. up to this working fine.
But, now I want to display a count down clock after selecting one option from above from that time onwords one clock, I want to display
That will useful to user how much time remaining to trigger the application
if any body knows about this please try to help me
Thanks for reading
Something like this for short counts:
public void timerCountDown(final int secondCount){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if (secondCount > 0){
//format time and display
counterBox.setText(Integer.toString((secondCount)));
timerCountDown(secondCount - 1);
}else{
//do after time is up
removeCounterViews();
}
}
}, 1000);
}
But this one is even better and it has two events which update the main UI thread: on tick and on finish, where tick's lenght is defined in the second parameter:
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();