Make a global timer - android

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

Related

Android count down timer explanation wanted

in the android reference thing i found a page about countdowntimers : CountDownTimers
it has this piece of code inside it:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
I wanted to know what it is and if its an object or class and how i can use it as a timer for objects of a different class. can someone please explain how i would put this into my android project and use it. thankyou.
the reference you quoted explains it very clear.
CountDownTimer is a CLASS and it's used to perform a determinated task until it's done.
The onTick method is used to perform an action on every tick (in milliseconds) and
public void onTick(long millisUntilFinished) {
//It will perform this task every millisecond until countdown finishes.
}
The onFinish method performs a last task until the class end's its instance.
public void onFinish() {
//It will perform this task only once, when the countdown finishes.
}
From the docs
public abstract class
CountDownTimer
extends Object
should answer one question.
As far as how to use it, see this answer

Stopwatch that works on background

Are there any sample stopwatch project ( or tutorial ) that stopwatch timer will be shown in notificationbar and will keep working even if application closed ?
A simple countdown timer will work, and will persist through onPause() if you allow it to.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Refer to this source for more information. You can also update TextViews if you need to show the timer.
Edit for count up specifically, try Chronometer, which can be found at this link. You can also find a demo of this in the Samples APK.

how can i set the time in digital clock

I want to build an android application, this application allow the users to answer some questions.
For every question, User have to answer it in 360 seconds, so I want to make a digital clock starts from 360secons and then down to 0 seconds
I read that I can't set time on digital clock , is it so?
or I have to use Chronometer?
this is my digital clock
DigitalClock timer;
public void init(){
timer=(DigitalClock)findViewById(R.id.dcTimer);
}
any help appreciated.
Use CountDownTimer instead.
That link also shows you how to update a TextView every 1 second.
Just change new CountDownTimer(30000, 1000) so the first parameter is 360000 instead of 30000 and that will give you a good start to your code.
EDIT : You need to import android.os.CountDownTimer and the code example I referred to is below. Good luck.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

How To reset Tick time in Countdowntimer On user Click

I want to reset Tick time in countdown timer on user click and user click is done in onTick()
event
please Help soon...
I think you should cancel The countdownTimer and initiate it with new Tick Time
new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish(){
Application.setContentView(layoutWithTheImage);
}
}.start();
And the you will set a click listener in the other layout... if you could wait until I will use pc, I'll give you all the code you need

how can i show elapsed time in text view from 1h 30min

i m creating an online examination app.
this app require to show elapsed time from 1h 30min.
what should i take timer or what?
elapsetime = (TextView)findViewById(R.id.txtElapsedTime);
elapsetime.setText(" "+hour+" h "+min+" m");
any suggestions?
Use CountDownTimer.
Use this : Example of showing a 30 second countdown
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
stop the Counter we use the countDownTimer.cancel()
if you want to Pause and Resume the timer refer below link
http://example.javamonday.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm
Refference : http://developer.android.com/reference/android/os/CountDownTimer.html

Categories

Resources