Last active
August 4, 2016 02:24
-
-
Save RowlandOti/86cab99038dff9c96d0dfe3e17fca7f2 to your computer and use it in GitHub Desktop.
QuizApp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.quizz; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
import android.widget.RadioGroup; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
int score = 0; | |
int fault = 0; | |
private RadioGroup radioGroupJourney; | |
private RadioGroup radioGroupTechnical; | |
private CheckBox chkBxNile; | |
private CheckBox chkBxEgyptRiver; | |
private CheckBox chkBxNepal; | |
private CheckBox chkBxAfrica; | |
private CheckBox chkBxEurope; | |
private CheckBox chkBxMiddleEast; | |
private CheckBox chkBxLearning; | |
private CheckBox chkBxEnjoying; | |
private CheckBox chkBxPlayFootball; | |
private CompoundButton.OnCheckedChangeListener chkCheckedListner; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
setupUI(); | |
} | |
public void setupUI() { | |
chkBxNile = (CheckBox) findViewById(R.id.nile); | |
chkBxEgyptRiver = (CheckBox) findViewById(R.id.egyptriver); | |
chkBxNepal = (CheckBox) findViewById(R.id.nepal); | |
chkBxAfrica = (CheckBox) findViewById(R.id.africa); | |
chkBxEurope = (CheckBox) findViewById(R.id.europe); | |
chkBxMiddleEast = (CheckBox) findViewById(R.id.middleeast); | |
chkBxLearning = (CheckBox) findViewById(R.id.learning); | |
chkBxEnjoying = (CheckBox) findViewById(R.id.enjoying); | |
chkBxPlayFootball = (CheckBox) findViewById(R.id.playingfootball); | |
radioGroupJourney = (RadioGroup) findViewById(R.id.journey); | |
radioGroupTechnical = (RadioGroup) findViewById(R.id.technical); | |
setListeners(); | |
} | |
public void setListeners() { | |
chkCheckedListner = new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton selectedcBox, boolean isChecked) { | |
// Check which checkbox was clicked | |
switch (selectedcBox.getId()) { | |
case R.id.nile: | |
if (isChecked) { | |
score += 1; | |
} | |
break; | |
case R.id.egyptriver: | |
if (isChecked) { | |
fault += 1; | |
} | |
break; | |
case R.id.nepal: | |
if (isChecked) { | |
fault += 1; | |
} | |
break; | |
case R.id.africa: | |
if (isChecked) { | |
score += 1; | |
} | |
break; | |
case R.id.europe: | |
if (isChecked) { | |
fault += 1; | |
} | |
break; | |
case R.id.middleeast: | |
if (isChecked) { | |
fault += 1; | |
} | |
break; | |
case R.id.learning: | |
if (isChecked) { | |
score += 1; | |
} | |
break; | |
case R.id.enjoying: | |
if (isChecked) { | |
fault += 1; | |
} | |
break; | |
case R.id.playingfootball: | |
if (isChecked) { | |
fault += 1; | |
} | |
break; | |
} | |
} | |
}; | |
chkBxNile.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxEgyptRiver.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxNepal.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxAfrica.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxEurope.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxMiddleEast.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxLearning.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxEnjoying.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxPlayFootball.setOnCheckedChangeListener(chkCheckedListner); | |
RadioGroup.OnCheckedChangeListener radioCheckChangeListener = new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) { | |
switch (radioGroup.getId()) { | |
case R.id.journey: | |
if (R.id.radio_yes == radioButtonId) { | |
score += 1; | |
} | |
if (R.id.radio_no == radioButtonId) { | |
fault += 1; | |
} | |
break; | |
case R.id.technical: | |
if (R.id.radio_pirates == radioButtonId) { | |
score += 1; | |
} | |
if (R.id.radio_ninjas == radioButtonId) { | |
fault += 1; | |
} | |
break; | |
} | |
} | |
}; | |
radioGroupTechnical.setOnCheckedChangeListener(radioCheckChangeListener); | |
radioGroupJourney.setOnCheckedChangeListener(radioCheckChangeListener); | |
} | |
public void display(String message) { | |
TextView ScoreView = (TextView) findViewById(R.id.socre_view); | |
ScoreView.setText(message); | |
ScoreView.setHighlightColor(Color.YELLOW); | |
} | |
public void submit(View view) { | |
display("the score is" + score + "\n the fualts is " + fault); | |
} | |
public void reset(View view) { | |
// ToDo: You might want to rest the view too | |
score = 0; | |
fault = 0; | |
display("the score is" + score + "\n the fualts is " + fault); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment