https://github.com/pars11/AndroidQuizApp
Android Quiz App is an quiz game for android. Dynamically as many categories and questions can be added, updated and deleted as desired. It currently uses Sqlite as the database.
When the game starts, the category selection screen appears. A new game is started with the questions in the category selected here. In each game questions are randomly brought.
When the game is over, a screen appears telling you how many questions you have made correctly and incorrectly and your score.
play_quiz_layout.xmlwas designed with radio buttons to allow users select only one answer from multi-choice answers in a question.PlayQuizAdapter.java has the logic underlying the radio group. In this class, the option values of radio button are displayed with the help of the adapter class.holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioButton = group.findViewById(checkedId);
String radioValue = radioButton.getText().toString().toLowerCase();
/* This sets each default value in the array e.g [0,0,0,0]
to 1 when a radiobutton is clicked
*/
answersQuestions.set(position, 1);
//This compares the answer from radiobutton with the answer in the database
if (radioValue.equals(data.getAnswerOoption())) {
if (selectedIds.get(position) != null){
if (!selectedIds.get(position).isCorrect){
correctScore += 1;
selectedIds.get(position).isCorrect = true;
}
} else {
AnswerModel model = new AnswerModel();
model.isCorrect = true;
model.id = group.getId();
selectedIds.set(position, model);
correctScore += 1;
}
} else {
if (selectedIds.get(position) != null){
if (selectedIds.get(position).isCorrect){
correctScore -= 1;
selectedIds.get(position).isCorrect = false;
}
} else {
AnswerModel model = new AnswerModel();
model.isCorrect = false;
model.id = group.getId();
selectedIds.set(position, model);
}
}
}
});
@Override
public int getItemCount() {
return questionList.size();
}
public int getCorrectScore() {
return correctScore;
}
public int getIncorrectScore() {
incorrectScore = questionList.size() - correctScore;
return incorrectScore;
}
void setValues(List<Question> values) {
questionList = values;
/**
* This gets the questions list size
* and generates a default array of integer
*/
int[] defaultValues = new int[questionList.size()];
for (int value : defaultValues) {
answersQuestions.add(value);
}
/**
* This generates a default array of AnswerModel with size() == questionList.size()
*/
for(int i = 0; i < questionList.size(); i++){
selectedIds.add(null);
}
notifyDataSetChanged();
}
PlayQuizFragment.java class, this has a Submit button clicked to submit quiz and view score using a customized alert dialog. Also, the customized alert dialog has options of exiting the quiz or playing the game again.@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
category = getActivity().getIntent().getExtras().getString("category");
fab = getActivity().findViewById(R.id.fab);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//This checks the array of set integer if it contains 0
// e.g 0 means radiobutton(option) not selected
boolean isAllQuestionsAnswered = playQuizAdapter.answersQuestions.contains(0);
if (isAllQuestionsAnswered) {
Toast.makeText(getContext(), "You cannot leave any blank question", Toast.LENGTH_SHORT).show();
} else {
mPresenter.calculateScore();
}
}
});
}
@Override
public void showQuestions(List<Question> questions) {
if (questions.isEmpty()) {
showEmptyMessage();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), AddQuestionActivity.class);
intent.putExtra("category", category);
startActivity(intent);
}
});
} else {
emptyTextView.setVisibility(View.GONE);
fab.setVisibility(View.GONE);
playQuizAdapter.setValues(questions);
}
}
@Override
public void showScore() {
//Create an alert dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
//Set title value
builder.setPositiveButton("Play Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(getActivity().getIntent());
}
})
.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getContext(), CategoriesActivity.class));
}
});
//Get custom view
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.score_dialog, null);
builder.setView(dialogView);
final TextView correctView = dialogView.findViewById(R.id.correct);
final TextView incorrectView = dialogView.findViewById(R.id.incorrect);
correctView.setText(String.format(Locale.ENGLISH, "%s: %d", "Correct", playQuizAdapter.getCorrectScore()));
incorrectView.setText(String.format(Locale.ENGLISH, "%s: %d", "Incorrect", playQuizAdapter.getIncorrectScore()));
alertDialog = builder.create();
alertDialog.show();
}