Esha Sharma
Esha Sharma

Reputation: 27

How to add timer to app

I am a new android developer. Recently, launched a word puzzle app - Wozzle

I want to add timer to the game so that a player gets a limited time to make a move. How to do that ? Also, the timer should be displayed to the player.

Upvotes: 1

Views: 6284

Answers (1)

Randroid
Randroid

Reputation: 3698

Try this,

public class CountDownTest extends Activity {

    TextView tv; // textview to display the countdown

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tv = new TextView(this);
        this.setContentView(tv);

        // 5000 is the starting number (in milliseconds)
        // 1000 is the number to count down each time (in milliseconds)
        MyCount counter = new MyCount(5000, 1000);

        counter.start();

    }

    //countdowntimer is an abstract class, so extend it and fill in methods
    public class MyCount extends CountDownTimer{

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            tv.setText("done!");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv.setText("Left: " + millisUntilFinished/1000);

        }

    }
}

Have a look at these links

Upvotes: 4

Related Questions