0

I am working on a quiz app where i am retrieving my data from my model class. Everything is working fine for now. However i want to implement button validation for my answers. For instance, when the user answers a question i want the correct answer(button) to blink green color and the wrong one to blink red color in case the user gets it wrong.

/ This file contains questions from QuestionBank
public class QuestionLibrary{
    // array of questions
    private String mQuestions [] = {
// my questions
 };
    // array of multiple choices for each question
    private String mChoices [][] = {
// array of choices appear here
               };
    // array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {
            // correct answers appear here
};



    // method returns number of questions
    public int getLength(){
        return mQuestions.length;
    }

    // method returns question from array textQuestions[] based on array index
    public String getQuestion(int a) {
        String question = mQuestions[a];
        return question;
    }

    // method return a single multiple choice item for question based on array index,
    // based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
    public String getChoice(int index, int num) {
        String choice0 = mChoices[index][num-1];
        return choice0;
    }

    //  method returns correct answer for the question based on array index
    public String getCorrectAnswer(int a) {
        String answer = mCorrectAnswers[a];
        return answer;
    }
}

public class QuizActivity extends AppCompatActivity {
    private QuestionLibrary mQuestionLibrary = new QuestionLibrary();

    Button button1;
    Button button2;
    Button button3;
    Button button4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begineer);

        button1 = (Button) findViewById(R.id.firstOption);
        button2 = (Button) findViewById(R.id.secondOption);
        button3 = (Button) findViewById(R.id.thirdOption);
        button4 = (Button) findViewById(R.id.fourthOption);
        updateQuestion(); //update question
        updateQuizNumber(mquizNumber);

    }

    private void updateQuizNumber(int mquizNumber) {
        msingleQuestion.setText("" + mquizNumber+"/"+mQuestionLibrary.getLength());
    }


    private void updateQuestion() {
        // check if we are not outside array bounds for questions
        if(mQuestionNumber<mQuestionLibrary.getLength() ){
            // set the text for new question, and new 4 alternative to answer on four buttons
           mQuestion.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
           button1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
            button2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
            button3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
           button4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
            mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
            mQuestionNumber++;
        }
        else {
           Intent intent = new Intent(this, MenuOptions.class);
//            intent.putExtra("score", mScore); // pass the current score to the second screen
            startActivity(intent);
        }
    }

    public void onClick(View view) {
        //all logic for all answers buttons in one method
        Button answer = (Button) view;
        // if the answer is correct, increase the score
        if (answer.getText() == mAnswer){
            Toast.makeText(BegineerActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
// i need to validate correct answer here by making the button blink green
        }else   {
            Toast.makeText(BegineerActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
// i need to validate wrong answer here by making the button blink red

        }
        if (mQuestionNumber < mQuestionLibrary.getLength()) {
            // once user answer the question, we move on to the next one, if any
            updateQuestion();
            updateQuizNumber(mquizNumber);
        } else {

//           
        }


    }



    }



}
Ben Ajax
  • 668
  • 2
  • 13
  • 27

3 Answers3

0

i want the correct answer(button) to blink green color and the wrong one to blink red color in case the user gets it wrong

You can change your button color like this

 if (answer.getText() == mAnswer){
    Toast.makeText(BegineerActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
    answer.setBackgroundColor(Color.GREEN); // change button color to green
 }else{
    // set button background color to red 
 }

To know how to blink a button, you may follow this answer

How to make a Button blink in Android?

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • thanks for the answer. but how exactly can i make the green color blink for a set time say 2 seconds before it moves to the next question – Ben Ajax Oct 04 '17 at 16:48
0

You can use it like below

    if (answer.getText() == mAnswer){
        Toast.makeText(BegineerActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
        answer.setBackgroundColor(Color.GREEN);// This will change button color to green
animation(); // this will blink button
     }else{
        // set button background color to red 
     }
private void animation(){
Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(200); //This will blink button for 2 seconds
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    answer.startAnimation(anim);
}

Let me know if you need any clarification. Thanks

Priya
  • 1,602
  • 4
  • 22
  • 37
  • 1
    the color change and animation work as expected but the color change is permanent. How do i make it appear for a set time say 2 seconds and thereafter revert to its default color? – Ben Ajax Oct 05 '17 at 10:18
0

There is a Handler that will help you. You can use it like this:

findViewById(R.id.textviewAnswer).setBackground(getResources().getDrawable(R.drawable.green_layout));
            Handler handler2 = new Handler();
            handler2.postDelayed(new Runnable() {
                public void run() {
                    findViewById(R.id.textviewAnswer).setBackground(getResources().getDrawable(R.drawable.white_layout));
                    Handler handler3 = new Handler();
                    handler3.postDelayed(new Runnable() {
                        public void run() {
                            findViewById(R.id.textviewAnswer).setBackground(getResources().getDrawable(R.drawable.green_layout));
                        }
                    }, 200);
                }
            }, 200);

this code will help you to blink your textview_layout or button for every 200 milliseconds. You can arrange the time in milliseconds and if you want to make more blink animation, you need to write more nested Handler-s

I wish it would be helpful