Skip to content

Instantly share code, notes, and snippets.

@RowlandOti
Last active August 10, 2016 16:01
Show Gist options
  • Save RowlandOti/b1246346872c01b8c50a214dcb719204 to your computer and use it in GitHub Desktop.
Save RowlandOti/b1246346872c01b8c50a214dcb719204 to your computer and use it in GitHub Desktop.
Josh Garcia Project
package com.example.android.project3;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// Score
private static int myScore = 0;
// Buttons
private Button submit;
// RadioGroup
private RadioGroup radioGroupRG1;
private RadioGroup radioGroupRG2;
// EditText
private EditText editTextFear;
// CheckBoxex
private CheckBox chkBxHan;
private CheckBox chkBxChewbacca;
private CheckBox chkBxLuke;
private CheckBox chkBxTrust;
private CheckBox chkBxDo;
private CheckBox chkBxFather;
// boolean for choices
private boolean isChoiceChewbacca = false;
private boolean isChoiceC3PO = false;
private boolean isChoiceVadar = false;
private boolean isChoiceDo = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Submit and Reset Buttons
submit = (Button) findViewById(R.id.Submit);
// Initialize the checkboxes
chkBxHan = (CheckBox) findViewById(R.id.Han);
chkBxChewbacca = (CheckBox) findViewById(R.id.Chewbacca);
chkBxLuke = (CheckBox) findViewById(R.id.Luke);
chkBxTrust = (CheckBox) findViewById(R.id.Trust);
chkBxDo = (CheckBox) findViewById(R.id.Do);
chkBxFather = (CheckBox) findViewById(R.id.Father);
//Initialize EditText
editTextFear = (EditText) findViewById(R.id.fear);
//Initialize RadioGroup 1
radioGroupRG1 = (RadioGroup) findViewById(R.id.rg_1);
radioGroupRG2 = (RadioGroup) findViewById(R.id.rg_2);
setListeners();
}
private class MyRadioGroupButtonCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) {
switch (radioGroup.getId()) {
case R.id.rg_1:
if (R.id.Jar == radioButtonId) {
isChoiceVadar = false;
}
if (R.id.Vadar == radioButtonId) {
isChoiceVadar = true;
showTextNotification("Hurray - Correct");
}
break;
case R.id.rg_2:
if (R.id.Lando == radioButtonId) {
isChoiceC3PO = false;
}
if (R.id.C3PO == radioButtonId) {
isChoiceC3PO = true;
showTextNotification("Hurray - Correct");
}
break;
}
}
}
private class MyCheckBoxCheckedChangeListener implements CheckBox.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView == chkBxChewbacca) {
if (!chkBxHan.isChecked()) {
isChoiceChewbacca = true;
showTextNotification("Hurray - Correct");
}
}
if (buttonView == chkBxLuke) {
if (!chkBxHan.isChecked()) {
isChoiceChewbacca = true;
showTextNotification("Hurray - Correct");
}
}
if (buttonView == chkBxHan) {
isChoiceChewbacca = false;
}
if (buttonView == chkBxDo) {
if (!chkBxTrust.isChecked() && !chkBxFather.isChecked()) {
isChoiceDo = true;
showTextNotification("Hurray - Correct");
}
}
if (buttonView == chkBxTrust) {
isChoiceDo = false;
}
if (buttonView == chkBxFather) {
isChoiceDo = false;
}
}
}
}
public void setListeners() {
chkBxChewbacca.setOnCheckedChangeListener(new MyCheckBoxCheckedChangeListener());
chkBxHan.setOnCheckedChangeListener(new MyCheckBoxCheckedChangeListener());
chkBxLuke.setOnCheckedChangeListener(new MyCheckBoxCheckedChangeListener());
chkBxTrust.setOnCheckedChangeListener(new MyCheckBoxCheckedChangeListener());
chkBxDo.setOnCheckedChangeListener(new MyCheckBoxCheckedChangeListener());
chkBxFather.setOnCheckedChangeListener(new MyCheckBoxCheckedChangeListener());
// RadioGroup Listeners
radioGroupRG1.setOnCheckedChangeListener(new MyRadioGroupButtonCheckedChangeListener());
radioGroupRG2.setOnCheckedChangeListener(new MyRadioGroupButtonCheckedChangeListener());
}
//Score button submits the score to display
public void submit(View view) {
if (isChoiceChewbacca) {
myScore += 2;
}
if (isChoiceVadar) {
myScore += 1;
}
if (isChoiceC3PO) {
myScore += 1;
}
if (isChoiceDo) {
myScore += 1;
}
boolean isTextFear = editTextFear.getText().toString().trim().equals("fear");
if (isTextFear) {
myScore += 1;
}
// Update Score
display(myScore);
// Disable Submit Button - Lock Submission
submit.setEnabled(false);
}
//Clear button resets all
public void reset(View view) {
myScore = 0;
// Reset the EditText
editTextFear.setText("");
// Reset all CheckBoxes
chkBxChewbacca.setChecked(false);
chkBxHan.setChecked(false);
chkBxLuke.setChecked(false);
chkBxTrust.setChecked(false);
chkBxDo.setChecked(false);
chkBxFather.setChecked(false);
isChoiceChewbacca = false;
isChoiceVadar = false;
isChoiceDo = false;
isChoiceC3PO = false;
// Reset all RadioButtons
radioGroupRG1.clearCheck();
radioGroupRG2.clearCheck();
// Reset Displayed score
display(myScore);
// Enable Submit Button -Unlock Submission
submit.setEnabled(true);
}
//Display the Score
public void display(int myScore) {
TextView scoreView = (TextView) findViewById(R.id.Display);
scoreView.setText(String.valueOf(myScore));
}
// Reveal correct answer -for Debugging
public void showTextNotification(String msgToDisplay) {
Toast.makeText(MainActivity.this, msgToDisplay, Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment