Task description
Make an quiz-app based upon a topic you feel familiar with!
Following requirements
Has at least four questions.
Following controls are all used at least one time: Button, TextView, EditText, CheckBoxes, RadioButtons.
At least one CheckBox-Group uses multi-select.
It's got to have a Submit-Button and a Reset-Button, which empties all controls.
Here's my Java source-code ...
package de.n_or.quizapp;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startEvaluation(View view) {
String[] answers = evaluateGui();
int result = evaluateQuiz(answers);
toastResult(result);
}
public String[] evaluateGui() {
String[] ret = new String[5];
EditText editTextQuestion1 = findViewById(R.id.question_1);
CheckBox checkBoxQuestion2Greece = findViewById(R.id.question_2_Greece);
CheckBox checkBoxQuestion2Burma = findViewById(R.id.question_2_Burma);
CheckBox checkBoxQuestion2Luxembourg = findViewById(R.id.question_2_Luxembourg);
Boolean answerQuestion2 = false;
if (checkBoxQuestion2Greece.isChecked() == true && checkBoxQuestion2Burma.isChecked() == false && checkBoxQuestion2Luxembourg.isChecked() == true) {
answerQuestion2 = true;
}
CheckBox checkBoxQuestion4Capital = findViewById(R.id.question_4_capital);
CheckBox checkBoxQuestion4Hessia = findViewById(R.id.question_4_hessia);
CheckBox checkBoxQuestion4Bavaria = findViewById(R.id.question_4_bavaria);
Boolean answerQuestion4 = false;
Boolean capital = checkBoxQuestion4Capital.isChecked();
Boolean hessia = checkBoxQuestion4Hessia.isChecked();
Boolean bavaria = checkBoxQuestion4Bavaria.isChecked();
if (capital == false && hessia == false && bavaria == true) {
answerQuestion4 = true;
}
ret[0] = editTextQuestion1.getText().toString().toLowerCase();
ret[1] = Boolean.toString(answerQuestion2);
ret[2] = evaluateRadioGroup(R.id.radio_group_question_3).toLowerCase();
ret[3] = Boolean.toString(answerQuestion4);
ret[4] = evaluateRadioGroup(R.id.radio_group_question_5).toLowerCase();
return ret;
}
public int evaluateQuiz(String[] answers) {
int result = 0;
String[] correctAnswers = {"paris", "true", "south", "true", "germany"};
for (int i = 0; i < correctAnswers.length; i++) {
if (answers[i].equals(correctAnswers[i])) {
result++;
}
}
return result;
}
public void toastResult(int result) {
String message = result + " out of 5. ";
if (result == 0) {
message += "Poor luck.";
} else if (result == 1) {
message += "You could do better.";
} else if (result == 2) {
message += "Quite nice.";
} else if (result == 3) {
message += "Really nice.";
} else if (result == 4) {
message += "Great!";
} else if (result == 5) {
message += "Absolutely awesome!";
}
Toast reportResult = Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT);
reportResult.show();
}
private String evaluateRadioGroup(int id) {
RadioGroup radioGroupQuestion;
RadioButton radioButtonQuestion;
radioGroupQuestion = findViewById(id);
int radioButtonId = radioGroupQuestion.getCheckedRadioButtonId();
radioButtonQuestion = findViewById(radioButtonId);
if (radioButtonQuestion == null) {
return "";
}
return (String)radioButtonQuestion.getText();
}
public void reset(View view) {
EditText editText = findViewById(R.id.question_1);
editText.setText("");
CheckBox checkBox = findViewById(R.id.question_2_Greece);
checkBox.setChecked(false);
checkBox = findViewById(R.id.question_2_Burma);
checkBox.setChecked(false);
checkBox = findViewById(R.id.question_2_Luxembourg);
checkBox.setChecked(false);
RadioGroup radioGroup = findViewById(R.id.radio_group_question_3);
radioGroup.clearCheck();
checkBox = findViewById(R.id.question_4_capital);
checkBox.setChecked(false);
checkBox = findViewById(R.id.question_4_hessia);
checkBox.setChecked(false);
checkBox = findViewById(R.id.question_4_bavaria);
checkBox.setChecked(false);
radioGroup = findViewById(R.id.radio_group_question_5);
radioGroup.clearCheck();
}
}
The XML for the layout ...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.n_or.quizapp.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:text="@string/sub_header"
android:textAlignment="center"
android:textSize="22sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:text="@string/question1"
android:textSize="16sp" />
<EditText
android:id="@+id/question_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:text="@string/question2"
android:textSize="16sp" />
<CheckBox
android:id="@+id/question_2_Greece"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="@string/checkbox_greece" />
<CheckBox
android:id="@+id/question_2_Burma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="@string/checkbox_burma" />
<CheckBox
android:id="@+id/question_2_Luxembourg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="@string/checkbox_luxembourg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:text="@string/question3"
android:textSize="16sp" />
<RadioGroup
android:id="@+id/radio_group_question_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/question_3_north"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:text="@string/north" />
<RadioButton
android:id="@+id/question_3_east"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="@string/east" />
<RadioButton
android:id="@+id/question_3_south"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="@string/south" />
<RadioButton
android:id="@+id/question_3_west"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="@string/west" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:text="@string/question4"
android:textSize="16sp" />
<CheckBox
android:id="@+id/question_4_capital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="@string/munich_question_1" />
<CheckBox
android:id="@+id/question_4_bavaria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="@string/munich_question_3" />
<CheckBox
android:id="@+id/question_4_hessia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="@string/munich_question_2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:text="@string/question5"
android:textSize="16sp" />
<RadioGroup
android:id="@+id/radio_group_question_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/question_5_germany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:text="@string/germany" />
<RadioButton
android:id="@+id/question_5_poland"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="@string/poland" />
<RadioButton
android:id="@+id/question_5_denmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="@string/denmark" />
</RadioGroup>
<Button
android:id="@+id/evaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="startEvaluation"
android:text="@string/evaluate_button" />
<Button
android:id="@+id/reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="reset"
android:text="@string/reset_button"
android:textColor="#ff0033"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Hyperlink to my GitHub-Repository.
What would you have done different and why?
Looking forward to reading for comments and answers.