Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is countdownTimer is not working?

I am trying, but unable, to create a counter that will show the remaining time in a text view after every minute. The time is not appearing, nor is there any error.

Here is my code:

package com.timer;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv);

    CountDownTimer timer = new CountDownTimer(30000, 1000) {

         public void onTick(long millisUntilFinished) {

             int seconds = (int) (millisUntilFinished / 1000);
             tv.setText("seconds remaining: " + String.valueOf(seconds));
         }

         public void onFinish() {
             tv.setText("Finished!!");
         }
      };

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

And my main XML layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</RelativeLayout>
like image 973
user3325349 Avatar asked Dec 06 '22 03:12

user3325349


2 Answers

you have to start your CountDownTimer. you missed timer.start();

like image 153
Blackbelt Avatar answered Dec 10 '22 12:12

Blackbelt


As per the official documentation, you need to call .start(), before the CountDownTimer will be enabled:

http://developer.android.com/reference/android/os/CountDownTimer.html

Specifically:

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
}.start();  // YOU'RE MISSING THIS CALL

So your code would become:

CountDownTimer timer = new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {

         int seconds = (int) (millisUntilFinished / 1000);
         tv.setText("seconds remaining: " + String.valueOf(seconds));
     }

     public void onFinish() {
         tv.setText("Finished!!");
     }
  }.start();
like image 40
fantasitcalbeast Avatar answered Dec 10 '22 13:12

fantasitcalbeast