-2

Im developing an application and I want to display Timer on the screen and do actions with it. I need that when the timer will stop the gamer will lose the game. That part I can do alone, but the part I dont know how to do is how to show the timer on screen and make the remaining time visible.

I hope you could help me with that and thank you for helping !

Omri Attiya
  • 3,917
  • 3
  • 19
  • 35

2 Answers2

1
new CountDownTimer(1000, 1000) {

    public void onTick(long millisecondsUntilFinished) {
         txtView.setText(String.valueOf(millisecondsUntilFinished/ 1000));
    }

    public void onFinish() {
    }
 }.start();
CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
  • thank you, its working. Do you know how can I add some time from inside this action? – Omri Attiya Jun 11 '14 at 13:09
  • Kindly accept it as the answer by ticking the right mark on the leftmost side. I don't understand what do you mean by adding time from inside the action. – CodeWarrior Jun 11 '14 at 13:24
  • i will tell you the background that you will understand: i have a game that you play with time limit. when the times up the games lose. i want that after the gamer will get 10 points in the game he will have more 5 seconds to play – Omri Attiya Jun 11 '14 at 13:31
0

Try This Code:-

    long totalTimeCountInMilliseconds = 60000;  // 1 minutes = 60000 milisecond 

private CountDownTimer countDownTimer;

    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
            // 500 means, onTick function will be called at every 500
            // milliseconds

            @Override
            public void onTick(long leftTimeInMilliseconds) {
                long seconds = leftTimeInMilliseconds / 1000;

                tv_CountDownTimer.setText("Counter"
                        + " "
                        + String.format("%02d:%02d:%02d", seconds / 3600,
                                (seconds % 3600) / 60, (seconds % 60)));
                // format the textview to show the easily readable format


            }

            @Override
            public void onFinish() {

            }

        }.start();
Rutvij
  • 493
  • 3
  • 10