Skip to content

Instantly share code, notes, and snippets.

@shamikalashawn
Created April 11, 2018 05:19
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/ae273d9b6f1b430a09e05619e0cba0e1 to your computer and use it in GitHub Desktop.
Save shamikalashawn/ae273d9b6f1b430a09e05619e0cba0e1 to your computer and use it in GitHub Desktop.
Kent Stole 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"/>
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text=""
android:textColor="@android:color/black"/>
<RadioGroup
android:id="@+id/all_radio_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<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"/>
<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Score: "
android:textSize="32sp"/>
</LinearLayout>
package com.shamikalashawn.quizapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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();
@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);
//select and display random question
pickQuestion();
//display score
displayScore();
}
/**
* 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();
//determine which radio button is checked and compare to question's correct answer
// boolean checked = ((RadioButton) view).isChecked();
//
// switch (view.getId()){
// case R.id.black_radio_button:
// if (checked && correctAnswer == "Black"){
// score += 10;
// uncheck(R.id.black_radio_button);
// Toast.makeText(getBaseContext(), "That's correct! You earned 10 more points!!", Toast.LENGTH_SHORT).show();
// break;
// }
//
// case R.id.gold_radio_button:
// if (checked && correctAnswer == "Gold"){
// score += 10;
// uncheck(R.id.gold_radio_button);
// Toast.makeText(getBaseContext(), "That's correct! You earned 10 more points!!", Toast.LENGTH_SHORT).show();
// break;
// }
//
// case R.id.green_radio_button:
// if (checked && correctAnswer == "Green"){
// score += 10;
// uncheck(R.id.green_radio_button);
// Toast.makeText(getBaseContext(), "That's correct! You earned 10 more points!!", Toast.LENGTH_SHORT).show();
// break;
// }
//
//
// case R.id.white_radio_button:
// if (checked && correctAnswer == "White"){
// score += 10;
// uncheck(R.id.white_radio_button);
// Toast.makeText(getBaseContext(), "That's correct! You earned 10 more points!!", Toast.LENGTH_SHORT).show();
// break;
// }
//
// case R.id.red_radio_button:
// if (checked && correctAnswer == "Red"){
// score += 10;
// uncheck(R.id.red_radio_button);
// Toast.makeText(getBaseContext(), "That's correct! You earned 10 more points!!", Toast.LENGTH_SHORT).show();
// break;
// }
//
// default:
// RadioGroup all = (RadioGroup) findViewById(R.id.all_radio_buttons);
// all.clearCheck();
// Toast.makeText(getBaseContext(), "Sorry, that is incorrect. We were looking for " + correctAnswer + ".", Toast.LENGTH_SHORT).show();
//
// }
RadioButton black = findViewById(R.id.black_radio_button);
RadioButton red = findViewById(R.id.red_radio_button);
RadioButton white = findViewById(R.id.white_radio_button);
RadioButton gold = findViewById(R.id.gold_radio_button);
RadioButton green = findViewById(R.id.green_radio_button);
RadioGroup all = findViewById(R.id.all_radio_buttons);
if (black.isChecked() && correctAnswer == "Black") {
score += 10;
displayResults("That's correct! You earned 10 more points!!");
} else if (red.isChecked() && correctAnswer == "Red") {
score += 10;
displayResults("That's correct! You earned 10 more points!!");
} else if (white.isChecked() && correctAnswer == "White") {
score += 10;
displayResults("That's correct! You earned 10 more points!!");
} else if (gold.isChecked() && correctAnswer == "Gold") {
score += 10;
displayResults("That's correct! You earned 10 more points!!");
} else if (green.isChecked() && correctAnswer == "Green") {
score += 10;
displayResults("That's correct! You earned 10 more points!!");
} else {
displayResults("Sorry, that is incorrect. We were looking for " + correctAnswer + ".");
}
//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) {
TextView questionTextView = findViewById(R.id.question);
questionTextView.setText(message);
}
/**
* This method displays the given text on the screen.
*/
private void displayResults(String message) {
TextView questionTextView = findViewById(R.id.results);
questionTextView.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() {
TextView scoreTextView = findViewById(R.id.score);
scoreTextView.setText("Score: " + score);
}
/**
* This method repopulates the hashMap of questions
*/
public void reset() {
usedQuestions = new ArrayList();
score = 0;
displayScore();
}
/**
* This method picks a random question
*/
public void pickQuestion() {
Integer usedQuestionsLength = usedQuestions.size();
if (usedQuestionsLength == numberOfQuestions) {
displayResults("That's the game! Your score is " + score + ". Let's play again!");
reset();
}
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 {
reset();
}
}
//add random question number to used questions array
usedQuestions.add(questionNumber);
//display random question
String randQuestion = questions.get(questionNumber).toString();
displayQuestion(randQuestion);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment