Skip to content

Instantly share code, notes, and snippets.

@susanas
Last active April 12, 2018 04:42
Show Gist options
  • Save susanas/9e5f71ea147c68777e7ee909bd9a3150 to your computer and use it in GitHub Desktop.
Save susanas/9e5f71ea147c68777e7ee909bd9a3150 to your computer and use it in GitHub Desktop.
This is my Quiz App project for the Grow With Google Challenge Scholarship: Android Basics Course
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
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"
android:fillViewport="true"
tools:context="com.example.android.braquizapp.MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Question 1. Radio button selection.-->
<TextView
android:id="@+id/questionOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/question_One"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent" />
<RadioGroup
android:id="@+id/radioQuestionOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/questionOne">
<RadioButton
android:id="@+id/radio_button_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qOne1"
android:textSize="14sp"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/correct_radio_button_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qOne2"
android:textSize="14sp"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="@+id/radio_button_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qOne3"
android:textSize="14sp"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
<!--Question 2. Checkbox selection.-->
<TextView
android:id="@+id/questionTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginTop="8dp"
android:text="@string/question_Two"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioQuestionOne" />
<CheckBox
android:id="@+id/correct_qTwoA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qT1"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/questionTwo"/>
<CheckBox
android:id="@+id/correct_qTwoB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qT2"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/correct_qTwoA"/>
<CheckBox
android:id="@+id/correct_qTwoC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qT3"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/correct_qTwoB"/>
<CheckBox
android:id="@+id/qTwoD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qT4"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/correct_qTwoC"/>
<CheckBox
android:id="@+id/qTwoE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qT5"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@+id/qTwoD"/>
<CheckBox
android:id="@+id/correct_qTwoF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qT6"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/qTwoE"/>
<!--Question 3. Checkbox selection.-->
<TextView
android:id="@+id/questionThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginTop="8dp"
android:text="@string/question_Three"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/correct_qTwoF" />
<CheckBox
android:id="@+id/correct_qThrA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qThree1"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/questionThree"/>
<CheckBox
android:id="@+id/correct_qThrB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qThree2"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/correct_qThrA"/>
<CheckBox
android:id="@+id/correct_qThrC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qThree3"
android:textSize="14sp"
android:onClick="onCheckBoxClicked"
app:layout_constraintTop_toBottomOf="@+id/correct_qThrB"/>
<CheckBox
android:id="@+id/qThrD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="@string/qThree4"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@+id/correct_qThrC"/>
<!--Question 4. Radio button selection.-->
<TextView
android:id="@+id/questionFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/question_Four"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/qThrD" />
<RadioGroup
android:id="@+id/radioQuestionFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/questionFour">
<RadioButton
android:id="@+id/correct_yes_radio_button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Si"
android:textSize="14sp"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="@+id/no_radio_button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No"
android:textSize="14sp"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
<!--Question 5. Radio button selection.-->
<TextView
android:id="@+id/questionFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/question_Five"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioQuestionFour" />
<RadioGroup
android:id="@+id/radioQuestionFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/questionFive">
<RadioButton
android:id="@+id/yes_radio_button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Si"
android:textSize="14sp"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="@+id/correct_no_radio_button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No"
android:textSize="14sp"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
<TextView
android:id="@+id/yourGrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginTop="8dp"
android:text="Your grade will be posted here after you click on the button."
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioQuestionFive" />
<LinearLayout
android:id="@+id/nameTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/yourGrade">
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your Name"
android:inputType="textCapWords" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nameTextInput">
<Button
android:id="@+id/gradeQuiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Grade Quiz"
android:onClick="gradeQuiz"/>
<Button
android:id="@+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Submit Grade"
android:onClick="submitGrade"
app:layout_constraintLeft_toLeftOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.braquizapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
package com.example.android.braquizapp;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int counterQ1 = 0;
int counterQ2 = 0;
int counterQ3 = 0;
int counterQ4 = 0;
int counterQ5 = 0;
int totalGrade;
public void onCheckBoxClicked (View view){
// Is the check box now checked?
boolean checked = ((CheckBox) view).isChecked();
// Add 1 point for each correct answer selected.
CheckBox correctAnswerTwoA = (CheckBox) findViewById(R.id.correct_qTwoA);
CheckBox correctAnswerTwoB = (CheckBox) findViewById(R.id.correct_qTwoB);
CheckBox correctAnswerTwoC = (CheckBox) findViewById(R.id.correct_qTwoC);
CheckBox correctAnswerTwoF = (CheckBox) findViewById(R.id.correct_qTwoF);
CheckBox correctAnswerThreeA = (CheckBox) findViewById(R.id.correct_qThrA);
CheckBox correctAnswerThreeB = (CheckBox) findViewById(R.id.correct_qThrB);
CheckBox correctAnswerThreeC = (CheckBox) findViewById(R.id.correct_qThrC);
// Which check box was checked?
switch(view.getId()) {
// Question 2: A, B, C, F are correct answers, add 1 point to total grade.
case R.id.correct_qTwoA:
if (checked) {
correctAnswerTwoA.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ2 = 1;
}
break;
case R.id.correct_qTwoB:
if (checked) {
correctAnswerTwoB.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ2 = counterQ2 + 1;
}
break;
case R.id.correct_qTwoC:
if (checked) {
correctAnswerTwoC.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ2 = counterQ2 + 1;
}
break;
case R.id.correct_qTwoF:
if (checked) {
correctAnswerTwoF.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ2 = counterQ2 + 1;
}
break;
case R.id.qTwoD:
if (checked) {
((CheckBox) view).setChecked(true);
Toast.makeText(this, "Nope... try again!", Toast.LENGTH_SHORT).show();
}
break;
// Question 3: A, B, C are correct answers, add 1 point to total grade.
case R.id.correct_qThrA:
if (checked) {
correctAnswerThreeA.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ3 = 1;
}
break;
case R.id.correct_qThrB:
if (checked) {
correctAnswerThreeB.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ3 = counterQ3 + 1;
}
break;
case R.id.correct_qThrC:
if (checked) {
correctAnswerThreeC.setChecked(true);
Toast.makeText(this, "Yes! You got it right!", Toast.LENGTH_LONG).show();
counterQ3 = counterQ3 + 1;
}
break;
}
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Add 1 point for each correct answer selected.
RadioButton correctAnswerOne = (RadioButton) findViewById(R.id.correct_radio_button_b);
RadioButton correctAnswerFour = (RadioButton) findViewById(R.id.correct_yes_radio_button4);
RadioButton correctAnswerFive = (RadioButton) findViewById(R.id.correct_no_radio_button5);
// Which radio button was clicked?
switch(view.getId()) {
// Question 1
case R.id.radio_button_a:
if (checked) {
((RadioButton) view).setChecked(true);
Toast.makeText(this, "Nope... try again!", Toast.LENGTH_SHORT).show();
if (counterQ1 == 1) {
// no point
counterQ1 = 0;
}
}
break;
case R.id.radio_button_c:
if (checked) {
((RadioButton) view).setChecked(true);
Toast.makeText(this, "Nope... try again!", Toast.LENGTH_SHORT).show();
if (counterQ1 == 1) {
// no point
counterQ1 = 0;
}
}
break;
case R.id.correct_radio_button_b:
if (checked) {
correctAnswerOne.setChecked(true);
counterQ1 = 1;
}
break;
// Question 4
case R.id.no_radio_button4:
if (checked) {
((RadioButton) view).setChecked(true);
if (counterQ4 == 1) {
// no point
counterQ4 = 0;
}
}
break;
case R.id.correct_yes_radio_button4:
if (checked) {
correctAnswerFour.setChecked(true);
Toast.makeText(this, "You got it!", Toast.LENGTH_SHORT).show();
counterQ4 = 1;
}
break;
// Question 5
case R.id.yes_radio_button5:
if (checked) {
((RadioButton) view).setChecked(true);
if (counterQ5 == 1) {
// no point
counterQ5 = 0;
}
}
break;
case R.id.correct_no_radio_button5:
if (checked) {
correctAnswerFive.setChecked(true);
Toast.makeText(this, "You got it!", Toast.LENGTH_SHORT).show();
counterQ5 = 1;
}
break;
}
Log.v("MainActivity", "Radio button points so far: "+ counterQ1 + counterQ2 + counterQ3 + counterQ4 + counterQ5);
}
// After pressing Grade Quiz button, this method calls createGradeSummary method to get string;
// then calls displayMessage to display string.
public void gradeQuiz(View view) {
EditText nameField = (EditText) findViewById(R.id.name_field);
String nameInput = nameField.getText().toString();
totalGrade = calculateGrade(counterQ1, counterQ2, counterQ3, counterQ4, counterQ5);
String yourGradeMessage = createGradeSummary (nameInput, totalGrade, counterQ1, counterQ2, counterQ3, counterQ4, counterQ5);
displayMessage(yourGradeMessage);
}
private int calculateGrade(int counterQ1, int counterQ2, int counterQ3, int counterQ4, int counterQ5) {
int calcGrade = counterQ1 + counterQ2 + counterQ3 + counterQ4 + counterQ5;
return calcGrade;
}
// Returns score total and breakdown in a string.
private String createGradeSummary(String nameField, int totalGrade, int counterQ1, int counterQ2, int counterQ3, int counterQ4, int counterQ5) {
return nameField + ", this is your total score: " + totalGrade + "\nTotal score breakdown:" + "\nQuestion 1: " + counterQ1 +
"\nQuestion 2: " + counterQ2 + "\nQuestion 3: " + counterQ3 + "\nQuestion 4: " + counterQ4 + "\nQuestion 5: " + counterQ5;
}
private void displayMessage(String message) {
TextView yourGradeTextView = (TextView) findViewById(R.id.yourGrade);
yourGradeTextView.setText(message);
}
public void submitGrade(View view){
EditText nameField = (EditText) findViewById(R.id.name_field);
String nameInput = nameField.getText().toString();
totalGrade = calculateGrade(counterQ1, counterQ2, counterQ3, counterQ4, counterQ5);
String yourGradeMessage = createGradeSummary (nameInput, totalGrade, counterQ1, counterQ2, counterQ3, counterQ4, counterQ5);
Intent intent = new Intent (Intent.ACTION_SENDTO);
intent.setData(Uri.parse ("mailto: "));
intent.putExtra(Intent.EXTRA_SUBJECT, "Your BraQuiz Score");
intent.putExtra(Intent.EXTRA_TEXT, yourGradeMessage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
<resources>
<string name="app_name">Brassiere History Trivia App</string>
<!--<string name="title_header">How much you know about brassieres?</string>-->
<string name="question_One">\n1. Bandeau is ... [chose one]</string>
<string name="qOne1">a popular 80s new wave band.</string>
<string name="qOne2">worn to cover breasts.</string>
<string name="qOne3">an island off coast of France.</string>
<string name="question_Two">2. Corsets … </string>
<string name="qT1">were used in the early 1500s in France.</string>
<string name="qT2">are made with a whalebone sewn into it.</string>
<string name="qT3">flattened and pushed women\'s breasts upward.</string>
<string name="qT4">are extremely comfortable.</string>
<string name="qT5">were adapted by African tribes in 1600s.</string>
<string name="qT6">were popular until the early 1900s.</string>
<string name="question_Three">3. Girdles ... </string>
<string name="qThree1">were first used in late 19th century.</string>
<string name="qThree2">forces the torso forward and the hips to jut outward.</string>
<string name="qThree3">can make a woman’s figure more of an "S" shape.</string>
<string name="qThree4">can make some figures into a “C” shape.</string>
<string name="question_Four">4. In 1911, “Brassiere” was added to the Oxford English Dictionary.</string>
<string name="question_Five">5. Brasserie is misspelled in this app.</string>
<string name="Si">True</string>
<string name="No">False</string>
<string name="toast_Five">Nope. Brasserie is French for "brewery”. Brassiere is spelled correctly.</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#e91e63</item>
<item name="colorPrimaryDark">#ad1457</item>
<item name="colorAccent">#f06292</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment