Get remaining time on CountdownTimer and use the remaining time as a score android
Asked Answered
W

2

6

So i have here Quiz App and have timer. So what i want to happen for example i have set the timer for 15 seconds and if the user answer the question in 5 seconds i want the 10 seconds ramaining become 10 points and it will add to previous score plus the score of you will get upon answering the questions. so for now i have this ...

        if(savedInstanceState!=null){
        //saved instance state data
        int exScore = savedInstanceState.getInt("score");
        scoreText.setText("Score: "+exScore);
    }

    Timer = new CountDownTimer(15000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tv_time.setText("" + millisUntilFinished / 1000);
            int progress = (int) (millisUntilFinished / 150);
            progressBar.setProgress(progress);

        }

        @Override
        public void onFinish() {
            progressBar.setProgress(0);
            timeUp(context);

        }
    }.start();

and here is the one for onclick. if the user answer correctly it will add 10points automatically

public void onClick(View view) {
        Button clicked = (Button) view;
        int exScore = getScore();

    if (clicked.getText().toString().equals(this.active_question.getAnswer()) ) {
        if (this.questions.size() > 0) {
                    setQuestion(questions.poll());
                    scoreText.setText("Score: " + (exScore + 10))


    } else  {
        CustomGameOver cdd = new CustomGameOver(PlayQuizActivity.this);
        cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        cdd.show();
        setHighScore();


        Timer.cancel();
        }

}

I dont have any idea on how to get the remaianing time on CountdownTimer and add it as a score when the answer is correct. could anyone please help me please.

Witticism answered 18/9, 2015 at 19:49 Comment(0)
A
9

Just use millisUntilFinished from onTick of the CountDownTimer

And the bonus will be millisUntilFinished/1000

P.S I think you better use a lower interval than 1000, so the ProgressBar will seem smoother.

Ambit answered 18/9, 2015 at 20:17 Comment(0)
F
4

All you need to do is declare a long variable timeleft in your MainActivity.

long timeleft; 

Then, when you create a new Timer, set the "onTick" override to update the timeleft variable each "onTick" (which in the following example is 1000 milliseconds )

    timer = new CountDownTimer(time, 1000) {
       @Override
        public void onTick(long millisecondsUntilFinished) {
            timeleft = millisecondsUntilFinished;
        }
      }

Your app can access then the variable timeleft every time you need to check how much time is left.

   score = score + timeleft / 1000; // divide by 1000 to get seconds

Have in mind that if you need to update the timer, you have to cancel it and create a new timer with the updated time left (and the same override);

       timeleft = timeleft + bonustime;  // (if you want to add bonus time, remember has to be in milliseconds)
       if( timer != null){ timer.cancel();} // better check first if the timer exists
       timer = new CountDownTimer(timeleft, 1000) {
            @Override
            public void onTick(long millisecondsUntilFinished) {
                timeleft = millisecondsUntilFinished;
            }
Fillister answered 2/9, 2017 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.