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 {
//
}
}
}
}