Home » Android » android – How to use a textView to countdown to zero?

android – How to use a textView to countdown to zero?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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..

How to&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();

Answer:

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.

Answer:

I agree with Guglielmo Moretti but if you still want to do it with TextView on your own than implement using the classic Observer design pattern.

You can have your custom textview like the code below,

 package com.example.stackoverflow;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.widget.TextView;

public class MyTextView extends TextView implements Observer{

    public MyTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        this.setText(""+((Integer)data).intValue());
    }

}

Also, implement mode with observalble like this,

package com.example.stackoverflow;

import java.util.Observable;

public class MyTextViewModel extends Observable {

    private int counter;

    public MyTextViewModel(int initnumber){
        this.counter = initnumber;
    }

    public void decrementCounter(){
        if(this.counter>0){
            this.counter--;
            setChanged();
            notifyObservers(this.counter);
        }
    }

}

Test Activity like this,

package com.example.stackoverflow;

import android.app.Activity;
import android.os.Bundle;

import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;

LinearLayout ll;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // tv = (TextView) findViewById(R.id.textView1);
    ll = (LinearLayout)findViewById(R.id.ll1);
    MyTextView mtv = new MyTextView(getApplicationContext());
    MyTextViewModel mm = new MyTextViewModel(10);
    mm.addObserver(mtv);
    mtv.setText("Not yet init");
    ll.addView(mtv);
    mm.decrementCounter(); // call this function in a thread and as many times as you want
}
}