Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a textView to countdown to zero?

I was wondering if there was anyway to use the timer class to assign to a textView in order to count down from 10 to 0 and display it in real time?

I've looked into it and found the Chronometer functions but what I understand is that it only counts up?

I'd like to learn how to do this, so is there any way to do it? and if so, how? I'm completely puzzled..

like image 850
Adariel Lzinski Avatar asked Sep 26 '12 19:09

Adariel Lzinski


People also ask

How do you use TextView?

Android App Development for BeginnersA TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.


2 Answers

if you need to countdown, there is the homonym class in the android.os Package called CountDownTimer.

Here (http://developer.android.com/reference/android/os/CountDownTimer.html)you can find the correct usage of this class. This is exactly what you need.

This class is present since API Level 1 so you can't fall into compatibility problems.

Example (from the docs)

new CountDownTimer(30000, 1000) {

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

    public void onFinish() {
        mTextField.setText("done!");
     }
 }.start();
like image 104
Yngwie89 Avatar answered Oct 19 '22 02:10

Yngwie89


Try CountDownAnimation. It does exactly what you need. The project includes a test.

If you use CountDownTimer with ticks every 1 second, you will not get the last tick. So, I recommend that you try CountDownAnimation which uses a Handler.

like image 43
IvanRF Avatar answered Oct 19 '22 03:10

IvanRF