Stop watch in android withtimer starting from the top - android

I want a stop watch for an android quiz application but it should run from the top.I mean it should set the time for example 2 hrs for the application and then should come down from 2 hrs to 0 as the quiz moves on..How can we implement this in android????

Try to use countdown timer.
Refer the link.
http://developer.android.com/reference/android/os/CountDownTimer.html
Hope following code helps you out :)
final TextView textView=(TextView)findViewById(R.id.textView1);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
textView.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
textView.setText("done!");
}
}
.start();
}
});

Related

How to Pause CountDownTimer in Android when onClick

I have a timer running for my IQ activity, and I want to Pause that timer when user click on Answer Button.
Bellow is my running timer and I have cancel timer when onlick but it's doesn't work for me:
timer = (TextView) findviewbyid(r.id.time)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
banana.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
CountDownTimer.cancel();
}
}
You can create a variable 'total' for total time and just set: total = millisUntilFinished in the onTick method. Check this answer: https://stackoverflow.com/a/6469166/2126403

How to call method every 30 second?

I am a new develop of Android. I want to create an Application about time. The question is "how to call method every 30 second?"
Example: Every 30 second the application will send some message.
I do not know how to do it firstly, I use this function
time = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
if(isDistanceStable()){
Toast.makeText(ChangeStatus.this, "Your speed is normal.", 3).show();
}else{
Toast.makeText(ChangeStatus.this, "Your speed is abnormal.", 3).show();
callManualRed();
}
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
time.cancel();
//intervalCheckDistance();
}
}.start();
}
but how to call it every 30 second. Please give me an example or some solutions to solve it. Thank you very much and Sorry with my English
cdt = new CountDownTimer(30000, 30000) {
public void onTick(long millisUntilFinished) {
// Method
}
public void onFinish() {
cdt.start(); // Call Again After 30 seconds
}
}.start();
Remember to call cdt.cancel(); when you want to end the timer

Android countdown timer display milliseconds?

I want a timer to pop up when I click a button that will count down from 3 seconds. And it does that fine but i want it to also show the milliseconds so when I click the button the text would go from 3.0 to 0.1. How would I add the milliseconds to the text view?
new CountDownTimer(1000, 3000) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();
This is what I have
Other SO questions suggest CountDownTimer doesn't do sub 1-second granularity well. Look into a different class, like TimerTask.
Otherwise, the following would work.
new CountDownTimer(3000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ "." + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();

Android countdown timer wont accept variable

i just started to learn creating android apps. I wanted to create a simple count down timer that takes a value from a edittext but countdown timer does not seem to run.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTxt = (TextView) findViewById(R.id.countDownView);
intervalTxt = (TextView) findViewById(R.id.intervalText);
findViewById(R.id.startBN).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
int testInt = 30;
//countDownTxt.setText(intervalTxt.getText());
int interval = Integer.parseInt(intervalTxt.getText().toString());
Log.d("buttonpressed", "interval for countdown is " + interval);
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
countDownTxt.setText(""+ millisUntilFinished / 1000);
}
public void onFinish() {
cancel();
}
}.start();
}
}
);
}
In particular, this program works only if i enter a numerical value such as 30000 in the 1st parameter of the CountDownTimer "cdt = new CountDownTimer(testInt, 1000)"
Can someone enlighten me please? Thank you!
"Doesn't work" how? You should post the error message you're getting or other symptoms of "doesn't work."
What's probably happening is CountDownTimer accepts only long values as the first parameter of its constructor. Not int values.
Change int testInt = 30 to long testLong = 10000.0f and see what happens.
The first parameter means milliseconds, by the way, so "30" isn't really going to get you much in the first place.
onTick() method is called in a separate Thread. But you don't have right to use setText() method outside the GUI Thread.
You must use a Handler object or Activity.postOnUiThread() method to execute something in the GUI Thread :
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
runOnUiThread(new Runnable() {
#Override
public void run() {
countDownTxt.setText("" + millisUntilFinished / 1000);
}
});
countDownTxt.setText(""+ millisUntilFinished / 1000);
}
public void onFinish() {
cancel();
}
}.start();
For more informations, read http://developer.android.com/guide/components/processes-and-threads.html#Threads

implement timer in my app

i am developing a game.i want to display score on button click.but it should be displayed only for few seconds.i want to implement timer in my app.but i dnt knw how to implement that.i searched in the google.but results were confusing me...given below is my code snippet.plz anybody help me...
OnClickListener clickball=new OnClickListener() {
#Override
public void onClick(View v) {
score=scorenumber.nextInt(9);
id=v.getId();
Log.v("", "u clicked me");
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
}
else if(id==R.id.ball3)
{
ball3.setText(Integer.toString(score));
}
else if(id==R.id.ball5)
{
ball5.setText(Integer.toString(score));
}
}
}
Something like
//Show score here
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//hide score here
}
}, 2000);
will hide your score after two seconds.
No need to go so deep in your case. Every view has postDelayed() method, that will run a custom code in UI thread after a set amount of time (in milliseconds). For example:
ball5.postDelayed(new Runnable(){
#Override
public void run() {
ball5.setText("");
}
}, 3000);
will clear "ball5" text after 3 seconds has passed
Another example
...
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
ball2.postDelayed(new Runnable(){
#Override
public void run() {
ball2.setText("");
}
}, 3000);
}
...
you can use CountDownTimer(); refer the documentation ,
Example :
new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
//hide your score here after 5 secondes (5000/1000)
}
}.start();
I think CountDownTimer is that what you need:http://developer.android.com/reference/android/os/CountDownTimer.html . It's quite simle for implementation
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Categories

Resources