Countdown Timer required on Android - android

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);
}

Related

How to just have one OnClick Countdown Timer running?

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

Android countdown

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)

How to show remaining time in digital clock?

am creating one application for student. I need to show digital clock with remaining time.
here i add digital clock
but it show system date
and i need to show Remaining time means suppose if exam time is 10 minute then it must show
time in reverse manner. Or i need to use another clock.
Any help is Appreciated.
Define this code in onCreate.
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000, 1000);
counter.start();
Constructor for CountDownTimer is as follow.
// 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!”); //TextView object should be defined in onCreate
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);// This will be called every Second.
}
}
Take a look at the CountDownTimer-class.

How to stop media recorder when it come two seconds in android

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();

Trying to set up a countdown timer in Android through multiple Activites

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?

Categories

Resources