jeudi 28 septembre 2017

Check Quiz Answer with color code

I got my little simple Quiz up and running but there is one more feature I would like to implement, color coding for correct and wrong answers. I have 4 Buttons that randomly display there answers. I would like to implement a highlighted button of the correct answer after I pressed the wrong answer that changes the button color to red. my code of this part so far:

// Check Answer.
public void checkAnswer(View view) {

    // Get clicked button.
    final Button answerBtn = (Button) findViewById(view.getId());
    String btnText = answerBtn.getText().toString();

    if (btnText.equals(rightAnswer)) {
        rightAnswerCount++;
        Toast.makeText(getApplicationContext(), "Correct", 
        Toast.LENGTH_SHORT).show();
        soundPlayer.playCorrectSound();
        answerBtn.setBackgroundColor(Color.parseColor("#3fd250"));

    } else {

        soundPlayer.playWrongSound(); // Wrong Sound.
        Toast.makeText(getApplicationContext(), "Wrong " + rightAnswer, 
        Toast.LENGTH_SHORT).show();
        answerBtn.setBackgroundColor(Color.parseColor("#da251c")); 
        quizLife--;

    }
}

So in the else part I would like to color code my correct answer to green. I get my questions and answers from a sqlite Database.

Aucun commentaire:

Enregistrer un commentaire