Create Counter Using TextView – Android

This is yet another simple Android tutorial that lets you display a counter with a delay of one second. This question was asked on StackOverflow here.

The example updates a TextView every one second until it has reached the count 0. The counter is initially set to start from 10.

int i = 10; //declare this globally
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        if(i != 0) { 
                text.append(" " + i);
                handler.postDelayed(this, 1000);
            } else {
                text.append(" Stopped");
    }, 1000);

Make sure that you declared int i = 10; globally. The answer was originally posted here.