Skip to content

Instantly share code, notes, and snippets.

@shamikalashawn
Last active May 13, 2018 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamikalashawn/032950acc6f1e432666c94b6525dd22e to your computer and use it in GitHub Desktop.
Save shamikalashawn/032950acc6f1e432666c94b6525dd22e to your computer and use it in GitHub Desktop.
Quiz App
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/kentecloth"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Your Knowledge of the Kente Stole"
android:textSize="24sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="@android:color/black"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/playerInput"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="text"
android:hint="Player Name"
android:layout_weight="3"/>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="submitPlayer"/>
</LinearLayout>
<TextView
android:id="@+id/question"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text=""
android:textColor="@android:color/black"/>
<CheckBox
android:id="@+id/checkYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:visibility="gone"
android:onClick="onCheck"/>
<CheckBox
android:id="@+id/checkNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:visibility="gone"
android:onClick="onCheck"/>
<RadioGroup
android:id="@+id/all_radio_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:visibility="gone"
>
<RadioButton
android:id="@+id/black_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black"
android:textAppearance="?android:textAppearanceMedium"
android:onClick="answerQuestion"/>
<RadioButton
android:id="@+id/gold_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gold"
android:textAppearance="?android:textAppearanceMedium"
android:onClick="answerQuestion"/>
<RadioButton
android:id="@+id/white_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="White"
android:textAppearance="?android:textAppearanceMedium"
android:onClick="answerQuestion"/>
<RadioButton
android:id="@+id/red_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:textAppearance="?android:textAppearanceMedium"
android:onClick="answerQuestion"/>
<RadioButton
android:id="@+id/green_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"
android:textAppearance="?android:textAppearanceMedium"
android:onClick="answerQuestion"/>
</RadioGroup>
<TextView
android:id="@+id/results"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="32sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/scoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:padding="8dp"
android:textSize="32sp"
android:layout_weight="3"/>
<Button
android:id="@+id/scoreButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:visibility="gone"
android:onClick="showScore"/>
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:visibility="gone"
android:onClick="reset"/>
</LinearLayout>
</LinearLayout>
package com.shamikalashawn.quizapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
import java.util.*;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Integer questionNumber = 0;
Integer score = 0;
Integer numberOfQuestions = 0;
ArrayList usedQuestions = new ArrayList();
HashMap questions = new HashMap();
HashMap answers = new HashMap();
RadioGroup radioAnswers;
TextView questionTextView;
TextView resultsTextView;
RadioButton black;
RadioButton red;
RadioButton white;
RadioButton gold;
RadioButton green;
RadioGroup all;
CheckBox yes;
CheckBox no;
Button resetButton;
String player;
EditText playerName;
Button submitButton;
TextView scoreTextView;
Button scoreButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
//populate list of questions
questions.put(1, "Which color represents posterity?");
questions.put(2, "Which color represents maturity?");
questions.put(3, "Which color represents pureness?");
questions.put(4, "Which color represents mourning?");
questions.put(5, "Which color represents spiritual growth?");
numberOfQuestions = questions.size();
//set answers
answers.put("Which color represents posterity?", "Gold");
answers.put("Which color represents maturity?", "Black");
answers.put("Which color represents pureness?", "White");
answers.put("Which color represents mourning?", "Red");
answers.put("Which color represents spiritual growth?", "Green");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioAnswers = (RadioGroup) findViewById(R.id.all_radio_buttons);
questionTextView = (TextView) findViewById(R.id.question);
resultsTextView = (TextView) findViewById(R.id.results);
black = findViewById(R.id.black_radio_button);
red = findViewById(R.id.red_radio_button);
white = findViewById(R.id.white_radio_button);
gold = findViewById(R.id.gold_radio_button);
green = findViewById(R.id.green_radio_button);
all = findViewById(R.id.all_radio_buttons);
yes = (CheckBox) findViewById(R.id.checkYes);
no = (CheckBox) findViewById(R.id.checkNo);
resetButton = (Button) findViewById(R.id.resetButton);
playerName = (EditText) findViewById(R.id.playerInput);
submitButton = (Button) findViewById(R.id.submitButton);
scoreTextView = findViewById(R.id.scoreTextView);
scoreButton = (Button) findViewById(R.id.scoreButton);
}
/**
* When radio button is clicked, this method checks the answer, updates the score, and displays a new question
*/
public void answerQuestion(View view) {
//capture correct answer for current question
String currentQuestion = questions.get(questionNumber).toString();
String correctAnswer = answers.get(currentQuestion).toString();
if (black.isChecked() && correctAnswer == "Black") {
score += 10;
Toast.makeText(getApplicationContext(), "That's correct! You earned 10 points!!",
Toast.LENGTH_LONG).show();
} else if (red.isChecked() && correctAnswer == "Red") {
score += 10;
Toast.makeText(getApplicationContext(), "That's correct! You earned 10 points!!",
Toast.LENGTH_LONG).show();
} else if (white.isChecked() && correctAnswer == "White") {
score += 10;
Toast.makeText(getApplicationContext(), "That's correct! You earned 10 points!!",
Toast.LENGTH_LONG).show();
} else if (gold.isChecked() && correctAnswer == "Gold") {
score += 10;
Toast.makeText(getApplicationContext(), "That's correct! You earned 10 points!!",
Toast.LENGTH_LONG).show();
} else if (green.isChecked() && correctAnswer == "Green") {
score += 10;
Toast.makeText(getApplicationContext(), "That's correct! You earned 10 points!!",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Sorry, that is incorrect. We were looking for " + correctAnswer + ".",
Toast.LENGTH_LONG).show();
}
//clear selection
all.clearCheck();
//update score
// displayScore();
//select a new question
pickQuestion();
}
/**
* This method displays the given text on the screen.
*/
private void displayQuestion(String message) {
questionTextView.setText(message);
}
/**
* This method displays the given text on the screen.
*/
private void displayResults(String message) {
resultsTextView.setText(message);
}
// /**
// * This method unchecks the radio button previously selected
// */
// private void uncheck(Integer buttonID) {
// RadioButton selectedRadioButton = (RadioButton) findViewById(buttonID);
// selectedRadioButton.toggle();
// }
/**
* This method displays the score.
*/
private void displayScore() {
scoreTextView.setText(player + "'s Score: " + score);
}
/**
* This method repopulates the hashMap of questions
*/
public void reset(View view) {
playerName.setText(null);
usedQuestions = new ArrayList();
score = 0;
displayQuestion("");
displayResults("");
yes.setChecked(false);
no.setChecked(false);
no.setClickable(true);
yes.setClickable(true);
yes.setVisibility(View.GONE);
no.setVisibility(View.GONE);
resetButton.setVisibility(View.GONE);
playerName.setVisibility(View.VISIBLE);
player = "";
scoreTextView.setVisibility(View.GONE);
displayScore();
submitButton.setVisibility(View.VISIBLE);
}
/**
* This method picks a random question
*/
public void pickQuestion() {
Integer usedQuestionsLength = usedQuestions.size();
Random rand = new Random();
questionNumber = rand.nextInt(numberOfQuestions) + 1;
//pick a random index and ensure it has not been used before
while (usedQuestions.contains(questionNumber)) {
if (usedQuestionsLength < numberOfQuestions) {
questionNumber = rand.nextInt(numberOfQuestions) + 1;
} else {
radioAnswers.setVisibility(View.GONE);
yes.setVisibility(View.VISIBLE);
no.setVisibility(View.VISIBLE);
displayQuestion("Did you enjoy playing?");
displayResults("That's the game!");
scoreButton.setVisibility(View.VISIBLE);
return;
}
}
//add random question number to used questions array
usedQuestions.add(questionNumber);
//display random question
String randQuestion = questions.get(questionNumber).toString();
displayQuestion(randQuestion);
}
/**
* This method occurs after checkbox is selected
*/
public void onCheck (View view){
switch (view.getId()) {
case R.id.checkNo:
if(no.isChecked())
displayResults("Aww, that's too bad! Maybe playing again will help.");
else
break;
case R.id.checkYes:
if(yes.isChecked())
displayResults("Sweet! Thanks for playing!");
else
break;
}
resetButton.setVisibility(View.VISIBLE);
no.setClickable(false);
yes.setClickable(false);
}
/**
* Method sets player name, makes question, radiogroup, and score visible
*/
public void submitPlayer(View view){
if (playerName.getText().toString().matches("")){
Toast.makeText(getApplicationContext(), "Please enter a player name!",
Toast.LENGTH_LONG).show();
}
else{
playerName.setVisibility(View.GONE);
player = playerName.getText().toString();
submitButton.setVisibility(View.GONE);
pickQuestion();
radioAnswers.setVisibility(View.VISIBLE);
}
// displayScore();
}
/**
* Method shows score after all questions have been answered
*/
public void showScore(View view){
scoreTextView.setVisibility(View.VISIBLE);
displayScore();
scoreButton.setVisibility(View.GONE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment