Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.htmlquiz;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- int step = 1;
- int correctAnswer = 0;
- int wrongAnswer = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- /**
- * Start the quiz showing the first question
- * @param v
- */
- public void startQuiz(View v){
- LinearLayout one = (LinearLayout) findViewById(R.id.start_quiz);
- one.setVisibility(View.GONE);
- LinearLayout question_1 = (LinearLayout) findViewById(R.id.question_1);
- question_1.setVisibility(View.VISIBLE);
- }
- /**
- * Check for answers, increase counters and go to next step
- * @param v
- */
- public void checkQuiz(View v){
- switch(step) {
- case 1:
- RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group_q1);
- if (radioGroup.getCheckedRadioButtonId() == -1) {
- // no radio buttons are checked
- }
- else {
- // one of the radio buttons is checked
- int selectedId = radioGroup.getCheckedRadioButtonId();
- RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
- String answer = selectedRadioButton.getText().toString();
- if(answer.equals("Both of the above")) {
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_1 = (LinearLayout) findViewById(R.id.question_1);
- question_1.setVisibility(View.GONE);
- LinearLayout question_2 = (LinearLayout) findViewById(R.id.question_2);
- question_2.setVisibility(View.VISIBLE);
- step += 1;
- }
- break;
- case 2:
- RadioGroup radioGroup2 = (RadioGroup) findViewById(R.id.radio_group_q2);
- if (radioGroup2.getCheckedRadioButtonId() == -1) {
- // no radio buttons are checked
- }
- else {
- // one of the radio buttons is checked
- int selectedId = radioGroup2.getCheckedRadioButtonId();
- RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
- String answer = selectedRadioButton.getText().toString();
- if(answer.equals("All of the above")) {
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_2 = (LinearLayout) findViewById(R.id.question_2);
- question_2.setVisibility(View.GONE);
- LinearLayout question_3 = (LinearLayout) findViewById(R.id.question_3);
- question_3.setVisibility(View.VISIBLE);
- step += 1;
- }
- break;
- case 3:
- RadioGroup radioGroup3 = (RadioGroup) findViewById(R.id.radio_group_q3);
- if (radioGroup3.getCheckedRadioButtonId() == -1) {
- // no radio buttons are checked
- }
- else {
- // one of the radio buttons is checked
- int selectedId = radioGroup3.getCheckedRadioButtonId();
- RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
- String answer = selectedRadioButton.getText().toString();
- if(answer.equals("date")) {
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_3 = (LinearLayout) findViewById(R.id.question_3);
- question_3.setVisibility(View.GONE);
- LinearLayout question_4 = (LinearLayout) findViewById(R.id.question_4);
- question_4.setVisibility(View.VISIBLE);
- step += 1;
- }
- break;
- case 4:
- RadioGroup radioGroup4 = (RadioGroup) findViewById(R.id.radio_group_q4);
- if (radioGroup4.getCheckedRadioButtonId() == -1) {
- // no radio buttons are checked
- }
- else {
- // one of the radio buttons is checked
- int selectedId = radioGroup4.getCheckedRadioButtonId();
- RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
- String answer = selectedRadioButton.getText().toString();
- if(answer.equals("All of the above")) {
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_4 = (LinearLayout) findViewById(R.id.question_4);
- question_4.setVisibility(View.GONE);
- LinearLayout question_5 = (LinearLayout) findViewById(R.id.question_5);
- question_5.setVisibility(View.VISIBLE);
- step += 1;
- }
- break;
- case 5:
- RadioGroup radioGroup5 = (RadioGroup) findViewById(R.id.radio_group_q5);
- if (radioGroup5.getCheckedRadioButtonId() == -1) {
- // no radio buttons are checked
- }
- else {
- // one of the radio buttons is checked
- int selectedId = radioGroup5.getCheckedRadioButtonId();
- RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
- String answer = selectedRadioButton.getText().toString();
- if(answer.equals("All of the above")) {
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_5 = (LinearLayout) findViewById(R.id.question_5);
- question_5.setVisibility(View.GONE);
- LinearLayout question_6 = (LinearLayout) findViewById(R.id.question_6);
- question_6.setVisibility(View.VISIBLE);
- step += 1;
- }
- break;
- case 6:
- RadioGroup radioGroup6 = (RadioGroup) findViewById(R.id.radio_group_q6);
- if (radioGroup6.getCheckedRadioButtonId() == -1) {
- // no radio buttons are checked
- }
- else {
- // one of the radio buttons is checked
- int selectedId = radioGroup6.getCheckedRadioButtonId();
- RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
- String answer = selectedRadioButton.getText().toString();
- if(answer.equals("geolocation.getCurrentPosition()")) {
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_6 = (LinearLayout) findViewById(R.id.question_6);
- question_6.setVisibility(View.GONE);
- LinearLayout question_7 = (LinearLayout) findViewById(R.id.question_7);
- question_7.setVisibility(View.VISIBLE);
- step += 1;
- }
- break;
- case 7:
- CheckBox a71 = (CheckBox) findViewById(R.id.a71);
- CheckBox a72 = (CheckBox) findViewById(R.id.a72);
- CheckBox a73 = (CheckBox) findViewById(R.id.a73);
- CheckBox a74 = (CheckBox) findViewById(R.id.a74);
- if(a71.isChecked() && a72.isChecked() && a74.isChecked()){
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_7 = (LinearLayout) findViewById(R.id.question_7);
- question_7.setVisibility(View.GONE);
- LinearLayout question_8 = (LinearLayout) findViewById(R.id.question_8);
- question_8.setVisibility(View.VISIBLE);
- step += 1;
- break;
- case 8:
- EditText et = (EditText) findViewById(R.id.missingTag);
- String answer = et.getText().toString();
- if(answer.equals("section")){
- correctAnswer += 1;
- } else {
- wrongAnswer += 1;
- }
- LinearLayout question_9 = (LinearLayout) findViewById(R.id.question_8);
- question_9.setVisibility(View.GONE);
- TextView success = (TextView) findViewById(R.id.success);
- success.setText("Correct answers: " + correctAnswer);
- TextView text_result = (TextView) findViewById(R.id.text_result);
- text_result.setText("Your final score is: " + correctAnswer);
- TextView wrong = (TextView) findViewById(R.id.wrong);
- wrong.setText("Wrong answers: " + wrongAnswer);
- LinearLayout final_result = (LinearLayout) findViewById(R.id.final_result);
- final_result.setVisibility(View.VISIBLE);
- Toast.makeText(getApplicationContext(), "Your finale score is; " + correctAnswer, Toast.LENGTH_LONG).show();
- step += 1;
- break;
- default:
- //setContentView(R.layout.default);
- }
- }
- /**
- * Reset status anc counters
- * @param v
- */
- public void restartQuiz(View v){
- /* Reset counters */
- this.step = 1;
- this.correctAnswer = 0;
- this.wrongAnswer = 0;
- /* Reset button status */
- RadioButton rb11 = (RadioButton) findViewById(R.id.a11);
- rb11.setChecked(true);
- RadioButton rb21 = (RadioButton) findViewById(R.id.a21);
- rb21.setChecked(true);
- RadioButton rb31 = (RadioButton) findViewById(R.id.a31);
- rb31.setChecked(true);
- RadioButton rb41 = (RadioButton) findViewById(R.id.a41);
- rb41.setChecked(true);
- CheckBox cb71 = (CheckBox) findViewById(R.id.a71);
- cb71.setChecked(false);
- CheckBox cb72 = (CheckBox) findViewById(R.id.a72);
- cb71.setChecked(false);
- CheckBox cb73 = (CheckBox) findViewById(R.id.a73);
- cb71.setChecked(false);
- CheckBox cb74 = (CheckBox) findViewById(R.id.a74);
- cb71.setChecked(false);
- EditText et = findViewById(R.id.missingTag);
- et.setText("");
- /* Hide results, show first step */
- LinearLayout one = (LinearLayout) findViewById(R.id.start_quiz);
- one.setVisibility(View.VISIBLE);
- LinearLayout final_result = (LinearLayout) findViewById(R.id.final_result);
- final_result.setVisibility(View.GONE);
- }
- }
Add Comment
Please, Sign In to add comment