Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the timer in android

I want to display the timer in TextView in the format of like [ 19:59].so when i click the start button ,the timer will display like this for example,i want to set upto 20 mintues,it will display like [19:58][19:87].can anyone give some ideas or example code?enter image description here

like image 731
rajeshlawrance Avatar asked Jul 05 '13 04:07

rajeshlawrance


People also ask

How do I get a countdown on my Android?

we can set count down time after completion of time it will stop and get 0 values. onTick(long millisUntilFinished ) - In this method we have to pass countdown mill seconds after done countdown it will stop Ticking. onFinish() - After finish ticking, if you want to call any methods or callbacks we can do in onFinish().

Where is the timer in Android?

Open your phone's Clock app . At the top, tap Timer. Enter how long you want the timer to run.

How do you show a timer?

TextView _tv = (TextView) findViewById( R. id. textView1 ); new CountDownTimer(20*60000, 1000) { public void onTick(long millisUntilFinished) { _tv. setText("seconds remaining: " +new SimpleDateFormat("mm:ss:SS").


1 Answers

You can use the CountDownTimer.

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

     TextView _tv = (TextView) findViewById( R.id.textView1 );
    new CountDownTimer(20*60000, 1000) {

        public void onTick(long millisUntilFinished) {
            _tv.setText("seconds remaining: " +new SimpleDateFormat("mm:ss:SS").format(new Date( millisUntilFinished)));
        }

        public void onFinish() {
           _tv.setText("done!");
        }
     }.start();

To cancel just call cancel on the timer.

public final void cancel()

Cancel the countdown.

like image 106
Raghunandan Avatar answered Oct 15 '22 04:10

Raghunandan