Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing MainActivity functionality
package com.example.android.quizapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitAnswers(View view) {
// Process student name field
EditText studentName = (EditText) findViewById(R.id.full_name_field);
String studentNameString = studentName.getText().toString();
if (studentNameString.isEmpty() || studentNameString.length() == 0 || studentNameString.equals("")) {
Log.i("MainActivity", "You didn't provide your full name");
} else {
Log.i("MainActivity", studentNameString);
}
// Processing Question 1:
CheckBox q1Answer1 = (CheckBox) findViewById(R.id.question_1_checkbox_1);
CheckBox q1Answer2 = (CheckBox) findViewById(R.id.question_1_checkbox_2);
CheckBox q1Answer3 = (CheckBox) findViewById(R.id.question_1_checkbox_3);
CheckBox q1Answer4 = (CheckBox) findViewById(R.id.question_1_checkbox_4);
CheckBox q1Answer5 = (CheckBox) findViewById(R.id.question_1_checkbox_5);
// Determining the correct answer for Question 1:
if (q1Answer1.isChecked() || q1Answer4.isChecked() || q1Answer5.isChecked() && (q1Answer2.isChecked() || q1Answer3.isChecked())) {
Log.i("MainActivity", "Q1: wrong answer");
} else if (q1Answer2.isChecked() && q1Answer3.isChecked()) {
Log.i("MainActivity", "Q1: Correct Answer");
} else if (q1Answer2.isChecked() || q1Answer3.isChecked()) {
Log.i("MainActivity", "Q1: Wrong Answer");
} else {
Log.i("MainActivity", "Q1: No Answer provided");
}
// Processing Question 2:
RadioButton q2Answer1 = (RadioButton) findViewById(R.id.question_2_radio_1);
RadioButton q2Answer2 = (RadioButton) findViewById(R.id.question_2_radio_2);
RadioButton q2Answer3 = (RadioButton) findViewById(R.id.question_2_radio_3);
// Determining the correct answer for Question 2:
if (q2Answer1.isChecked()) {
Log.i("MainActivity", "Q2: Correct Answer");
} else if (q2Answer2.isChecked() || q2Answer3.isChecked()) {
Log.i("MainActivity", "Q2: Wrong Answer");
} else {
Log.i("MainActivity", "Q2: No Answer provided!");
}
// Processing Question 3:
RadioButton q3Answer1 = (RadioButton) findViewById(R.id.question_3_radio_1);
RadioButton q3Answer2 = (RadioButton) findViewById(R.id.question_3_radio_2);
RadioButton q3Answer3 = (RadioButton) findViewById(R.id.question_3_radio_3);
// Determining the correct answer for Question 3:
if (q3Answer2.isChecked()) {
Log.i("MainActivity", "Q2: Correct Answer");
} else if (q3Answer1.isChecked() || q3Answer3.isChecked()) {
Log.i("MainActivity", "Q2: Wrong Answer");
} else {
Log.i("MainActivity", "Q3: No Answer provided!");
}
// Processing Question 4:
CheckBox q4Answer1 = (CheckBox) findViewById(R.id.question_4_checkbox_1);
CheckBox q4Answer2 = (CheckBox) findViewById(R.id.question_4_checkbox_2);
CheckBox q4Answer3 = (CheckBox) findViewById(R.id.question_4_checkbox_3);
// Determining the correct answer for Question 1:
if (q4Answer1.isChecked() || q4Answer2.isChecked()) {
Log.i("MainActivity", "Q4: wrong answer");
} else if (q4Answer3.isChecked()) {
Log.i("MainActivity", "Q4: Correct Answer");
} else {
Log.i("MainActivity", "Q4: No Answer provided!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.