Oz Giat
Oz Giat

Reputation: 127

Add a timer and freeze app (android studio)

im writing a quiz app on android studio, now, my goal is to add a time penalty for each wrong answer, means, if the user answered wrong he will have to wait lets say 10 sec' until he will be able to answer again, on those 10 sec', the app will freeze and the user will have to wait.

i want to show the time remaining.

the timer should be inserted in this part of the code:

    butNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
            RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
            Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
            if (currentQ.getANSWER().equals(answer.getText())) {//bad equal to
                score++;
                Log.d("good answer", "Your score" + score);
            }else {
                Log.d("bad answer", "Your score" + score);
               ***//the time should be implemented here***
            }
            if (qid < 7) {
                currentQ = quesList.get(qid);
                setQuestionView();
            } else {
                Intent intent = new Intent(Questionsctivity.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            }
        }
    });

thanks

Upvotes: 2

Views: 2235

Answers (3)

Lucas
Lucas

Reputation: 441

try this:

new Handler().postDelayed(new Runnable() {
                public void run() {
                // code called after 5 seconds...
                }
            }, 5 * 1000);

Upvotes: 2

ThaiPD
ThaiPD

Reputation: 3751

Below is my solution, it's easy to understand. Hope it help.

new CountDownTimer(30000, 1000) {

            @Override
            public void onFinish() {
                mTextField.setText("done!");
                // enable your view by butNext.setOnClickListener(listener) or radioGroup.setEnable(true)...
            }

            @Override
            public void onTick(long millisUntilFinished) {
                mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                // disable your view by butNext.setOnClickListener(null) or radioGroup.setEnable(false)...
            }
        };

Upvotes: 0

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

You can disable the Click Listeners and all the listeners for the specific time and you can continue to show the remaining time counter in TextView, when 10 seconds reach, you can enable the click listeners.

hope this helps.

Upvotes: 0

Related Questions