Reputation: 21
I am Developing a Quiz Game android app which contains Questions with radio Buttons and also a Button (Next).So when the button is pressed I have to check whether the user choose an answer from the choices.If not then an alert message should pop up stating that "You must choose your answer before proceeding to the next question". Can anyone please help me! Here is the design of my Quiz App
Here is the Java code.
public class Main2Activity extends AppCompatActivity {
Button button2;
private TextView countLabel;
private TextView questionLabel;
private RadioButton answerBtn1;
private RadioButton answerBtn2;
private RadioButton answerBtn3;
private RadioButton answerBtn4;
private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
static final private int QUIZ_COUNT = 10;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
//{"Question", "Right Answer", "Choice1", "Choice2", "Choice3"}
{"It is a stack of software for mobile devices which includes an Operating System, middleware and some key applications.", "Android", "Intent", "Toast", "Service"},
{"Developed by Microsoft.", "Windows", "Android", "Blackberry", "iOS"},
{"Used to build apps and run them directly on Apple devices.", "Xcode", "Android Studio", "Visual Studio", "Windows"},
{"Represents a single screen with a user interface", "Activity", "Service", "Content Provider", "Broadcast receivers"},
{"A component that runs in the background to perform operations or to perform work for remote processes", "Service", "Activity", "Content Provider", "Broadcast receivers"},
{"Managers a shared set of app data", "Content Provider", "Activity", "Service", "Broadcast receiver"},
{"Responds to system-wide Broadcast announcements", "Broadcast receivers", "Activity", "Content Provider", "Service"},
{"It is connected to either the external world of application or internal world of application", "Intent", "Manifest File", "Resources", "Broadcast receiver"},
{"It is a collection of views and other child views, it is an invisible part and the base class for layouts.", "viewGroup", "view", "Relative Layout", "Linear Layout"},
{"It will show a pop up message on the surface of the window", "Toast Notification", "Status Bar Notification", "Dialogue Notification", "Action Bar"},
{"It will show notifications on status bar", "Status Bar Notification", "Toast Notification", "Dialogue Notification", "Action Bar"},
{"It is an activity related notification.", "Dialogue Notification", "Toast Notification", "Status Bar Notification", "Action Bar"},
{"Contains code to test your application projects", "Test Module", "Library Module", "App Engine Module", "Android App Module"},
{"Cannot be installed onto a device", "Library Module", "Test Module", "Android App Module", "App Engine Module"},
{"Allows implementation of functionality such as Real-time interactions", "App Engine Module", "Test Module", "Library Module", "Android App Module"}
};`enter code here`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
countLabel = (TextView) findViewById(R.id.countLabel);
questionLabel = (TextView) findViewById(R.id.questionLabel);
answerBtn1 = (RadioButton) findViewById(R.id.answerBtn1);
answerBtn2 = (RadioButton) findViewById(R.id.answerBtn2);
answerBtn3 = (RadioButton) findViewById(R.id.answerBtn3);
answerBtn4 = (RadioButton) findViewById(R.id.answerBtn4);
button2 = (Button) findViewById(R.id.button2);
for (int i = 0; i < quizData.length; i++) {
ArrayList<String> tmpArray = new ArrayList<>();
tmpArray.add(quizData[i][0]);
tmpArray.add(quizData[i][1]);
tmpArray.add(quizData[i][2]);
tmpArray.add(quizData[i][3]);
tmpArray.add(quizData[i][4]);
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz() {
countLabel.setText("Question No." + "" + quizCount);
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
ArrayList<String> quiz = quizArray.get(randomNum);
questionLabel.setText(quiz.get(0));
rightAnswer = quiz.get(1);
quiz.remove(0);
Collections.shuffle(quiz);
answerBtn1.setText(quiz.get(0));
answerBtn2.setText(quiz.get(1));
answerBtn3.setText(quiz.get(2));
answerBtn4.setText(quiz.get(3));
answerBtn1.setChecked(false);
answerBtn2.setChecked(false);
answerBtn3.setChecked(false);
answerBtn4.setChecked(false);
quizArray.remove(randomNum);
}
public void checkAnswer(final View view) {
button2.setOnClickListener(new View.OnClickListener() {
final RadioButton answerBtn = (RadioButton) findViewById(view.getId());
String btnText = answerBtn.getText().toString();
private Boolean validationSuccess() {
if(answerBtn.isChecked()==false){
alertDialog();
return false;
}
return true;
}
@Override
public void onClick(View v) {
if (btnText.equals(rightAnswer)) {
rightAnswerCount++;
}else{
validationSuccess();
}
if (quizCount == QUIZ_COUNT) {
Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
startActivity(intent);
} else {
quizCount++;
showNextQuiz();
}
}
});
}
private void alertDialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Main2Activity.this);
alertDialogBuilder.setMessage("Please ensure that you choose your answer!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
Upvotes: 0
Views: 831
Reputation: 505
you can simply check the state of radio button by this method
radioButton.isChecked()
Upvotes: 0
Reputation: 12803
If not then an alert message should pop up stating that "You must choose your answer before proceeding to the next question"
Use RadioGroupButton
to group your radio button
and add below code in your button click listener
if (radioGroup.getCheckedRadioButtonId() == -1)
{
// no radio buttons are checked
// You must choose your answer before proceeding to the next question
}
else
{
// one of the radio buttons is checked
}
Upvotes: 3