I want to write a countdown in android which starts counting from 3 to 0. Like at first 3 in appearing and then disappearing and 2 is appearing and so on. I searched a lot but I couldn't find any good sample. Can you help me that what should I do?
use CountDownTimer
For example:
import android.os.CountDownTimer;
MyCount timerCount;
public class TestCountdown extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timerCount = new MyCount(3 * 1000, 1000);
timerCount.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
//some script here
}
#Override
public void onTick(long millisUntilFinished) {
//some script here
}
}
}
The good guys in Android thought about you.
You have a class for that - CountDownTimer.
I am not going to write you the code for this but this should not be difficult.Just use a thread to display the value 3(using say TextView) first then sleep for say (100ms assuming you want it to change after 1 sec) then decrease it and repeat.
An example would be
for i=0 to 3
print the number
thread.sleep(100)
Related
I am having trouble thinking of a way to keep only one CountdownTimer going for an onClick listener. Basically I have an application that 6 buttons and each time you hit one of the buttons it starts a countdown on the respective buttons text. However, I only want the most recent countdown started by an OnClick for each button running and updating the text on the button.
Here's the code I have so far... It works, but the countdowns start "fighting" with each other over which is altering the text of the button. Is there a way so that each time the OnClick is registered that any countdowns created by earlier button clicks are destroyed / ignored?
oBlue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Button was clicked so start our count down
CountDownTimer blueTime = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
String s = String.valueOf(millisUntilFinished / 1000);
oBlue.setText(s);
}
public void onFinish() {
oBlue.setText(String.valueOf(300));
}
}.start();
Replace your counter variable with an internal class, so that you do not need to create counter variable each time. Just create a time counter variable and call the start of it if you want to restart the counter each button.
Es:
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//enter your code
I´am designing a countdown timer, that works perfectly but only If I stay in this Activity. Because, if the countdown starts and I go back for example and start other Activity, the countdonws follows counting down. And logically if I start again the activty where the countdown timer is, it appears like nothing is counting down, but in the bacground is counting down, I know because on the ontick of the timer, I throw a continue notification to show it at the notification tab.
The code is bellow, sendnotification method is a method that throws a alert when the time finishes, and the sendnotificationTiempo to send a notification to the tab every second to show at the top.
public static final int NOTIFICATION_ID = 33;
public static final int NOTIFICATION_ID_Tiempo = 34;
private int minCrono;
TextView text;
MyCounter timer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StartCrono = (Button) findViewById(R.id.butt_start);
CancelCrono = (Button) findViewById(R.id.butt_cancel);
text=(TextView)findViewById(R.id.text_countTime1);
CancelCrono.setEnabled(false);
minCrono = mins.getCurrentItem();
StartCrono.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
minCrono=(60000*minCrono);
timer = new MyCounter(minCrono,1000);
timer.start();
}
});
CancelCrono.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timer.cancel();
text.setText("El tiempo se ha cancelado");
minutosCrono = mins.getCurrentItem();
if (mNotificationManager != null)
mNotificationManager.cancel(NOTIFICATION_ID_Tiempo);
}
});
}
Countdown Class:
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
StartCrono.setEnabled(true);
CancelCrono.setEnabled(false);
mins.setEnabled(true);
minCrono = mins.getCurrentItem();
//
text.setText("Time Complete!");
sendnotification("Guau", "Time finished.");
}
#Override
public void onTick(long millisUntilFinished) {
long segRestantesTotal=(millisUntilFinished/1000);
long minRestantes= (segRestantesTotal/60);
long segRestantes= (segRestantesTotal%60);
if(segRestantes>=10){
text.setText(minRestantes+":"+segRestantes);
sendnotificationTiempo("Guau", minRestantes+":"+segRestantes);
}
else{
text.setText(minRestantes+":0"+segRestantes);
sendnotificationTiempo("Guau", minRestantes+":0"+segRestantes);
}
}
}
}
So what I would to do, is to know when I go back the Activity and I start another one, to show the time what is counting down at the background to show it for example in a textview.
Or maybe if I can get the time that is counting down from the notification tab perfect.
Thanks!
Move your MyCounter instance out of the Activity. When the Activity restarts, the instance is being recreated so the old one is gone. There are a couple ways you can do this, but you need to have your MyCounter instance live somewhere that the Activity can access it but isn't responsible for its life cycle. I use a rudimentary ServiceLocator implementation that I wrote, but you can easily create a custom Application class and keep the timer (or a controller class that creates and controls the timer) there.
How do I stop the media recorder when it comes two minutes in android? I used stMaxduration and info listener, there is no guarantee in the call in the api.
Can anybody tell me any other way to achieve this? Provide code please
Thanks
The best way is to implement a countdown timer where you need to execute something after definite times. Below is the code. Here 120000 milliseconds represents two minutes and 1000 milliseconds represents 1 second "MyCount(120000, 1000)"
MyCount counter = new MyCount(120000, 1000);
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
//do your stuff here
}
#Override
public void onTick(long millisUntilFinished) {
}
}
Run using the threads when you stop the thread the media player will gets stop.
snippet:
Runnable r = new Runnable()
{
public void run()
{
mp1.start();
}
};
new Thread(r).start();
I'm trying to create a app for airports, that the top line will always display a countdown timer to when the plane leaves. This is the code I had.
This class is declared in each activity class. /countdowntimer is an abstract class, so extend it and fill in methods:
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
tv.setText(”done!”);
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);
}
}
But I wanted it to run on all the activities. I was thinking about having the notification manger to start a thread every 10 seconds, but I cannot access the UI memory on its thread.
Does anybody have a good solution to how to do this?
Here is the link where i have discussed about a countdown timer of format(mm:ss) in Java:
Java console code for StopWatch/Timer?
Now i want to display (and update) it in a textview in android. Any clue?
To count down
You'll use a TextField and update its content using CountDownTimer, or check Rahul's answer in this same question.
To count up
In your Activity
import android.widget.Chronometer;
...
private Chronometer crono;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_crono);
this.crono = (Chronometer) findViewById(R.id.calling_crono);
startCrono();
}
public void startCrono() {
crono.setBase(SystemClock.elapsedRealtime());
crono.start();
}
To stop it
crono.stop();
In the XML Layout screen_crono
<Chronometer
android:id="#+id/calling_crono"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="14sp"/>
To set it inside a TextView, I don't think that's possible. Place it to its right or to its left, depending on what you want.
If this is not what you wanted, I hope it helps someone else.
You can also use the CountDownTimer class available in android.
Just declare a constructor and start the timer with
timer test=new timer(30000,1000);
onTick will get fired once in every 1000ms in the above case. You can update your TextView from here
class timer extends CountDownTimer
{
public timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish()
{
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
// Update your textview on on tick
}
}
Use the method of a timed UI update in this article it really works great
http://developer.android.com/resources/articles/timed-ui-updates.html
Also, you might use the built in android time class to display your text, it will make your formatting much easier imo
http://developer.android.com/reference/android/text/format/Time.html
try this code.....
tv = new TextView(this);
this.setContentView(tv);
//10000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(10000,1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onFinish()
{
tv.setText("Time Up!");
}
public void onTick(long millisUntilFinished)
{
tv.setText("Time Left : " + millisUntilFinished/1000);
}