Guest User

Untitled

a guest
Feb 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. public class QuizActivity extends AppCompatActivity {
  2. public static final String EXTRA_SCORE = "extraScore";
  3. private static final long COUNTDOWN_IN_MILLIS = 30000;
  4.  
  5. private static final String KEY_SCORE = "keyScore";
  6. private static final String KEY_QUESTION_COUNT = "keyQuestionCount";
  7. private static final String KEY_MILLIS_LEFT = "keyMillisLeft";
  8. private static final String KEY_QUESTION_LIST = "keyQuestionList";
  9. private static final String KEY_ANSWERED = "keyAnswered";
  10.  
  11. private TextView textViewQuestion;
  12. private TextView textViewScore;
  13. private TextView textViewQuestionCount;
  14. private TextView textViewCountDown;
  15. private RadioGroup rbGroup;
  16. private RadioButton rb1;
  17. private RadioButton rb2;
  18. private RadioButton rb3;
  19. private Button buttonConfirmNext;
  20.  
  21. private ColorStateList textColorDefaultRb;
  22. private ColorStateList textColorDefaultCd;
  23.  
  24. private CountDownTimer countDownTimer;
  25. private long timeLeftInMillis;
  26.  
  27. private ArrayList<Question> questionList;
  28. private int questionCounter;
  29. private int questionCountTotal;
  30. private Question currentQuestion;
  31.  
  32. private int score;
  33. private boolean answered;
  34.  
  35. private long backPressedTime;
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_quiz);
  41.  
  42. textViewQuestion = findViewById(R.id.text_view_question);
  43. textViewScore = findViewById(R.id.text_view_score);
  44. textViewQuestionCount = findViewById(R.id.text_view_question_count);
  45. textViewCountDown = findViewById(R.id.text_view_countdown);
  46. rbGroup = findViewById(R.id.radio_group);
  47. rb1 = findViewById(R.id.radio_button1);
  48. rb2 = findViewById(R.id.radio_button2);
  49. rb3 = findViewById(R.id.radio_button3);
  50. buttonConfirmNext = findViewById(R.id.button_confirm_next);
  51.  
  52. textColorDefaultRb = rb1.getTextColors();
  53. textColorDefaultCd = textViewCountDown.getTextColors();
  54.  
  55. if (savedInstanceState == null) {
  56. QuizDbHelper dbHelper = new QuizDbHelper(this);
  57. questionList = dbHelper.getAllQuestions();
  58. questionCountTotal = questionList.size();
  59. Collections.shuffle(questionList);
  60.  
  61. showNextQuestion();
  62. } else {
  63. questionList = savedInstanceState.getParcelableArrayList(KEY_QUESTION_LIST);
  64. score = savedInstanceState.getInt(KEY_SCORE);
  65. questionCounter = savedInstanceState.getInt(KEY_QUESTION_COUNT);
  66. questionCountTotal = questionList.size();
  67. timeLeftInMillis = savedInstanceState.getLong(KEY_MILLIS_LEFT);
  68. answered = savedInstanceState.getBoolean(KEY_ANSWERED);
  69.  
  70. restoreQuestion();
  71. }
  72.  
  73. buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View v) {
  76. if (!answered) {
  77. if (rb1.isChecked() || rb2.isChecked() || rb3.isChecked()) {
  78. checkAnswer();
  79. } else {
  80. Toast.makeText(QuizActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
  81. }
  82. } else {
  83. showNextQuestion();
  84. }
  85. }
  86. });
  87. }
  88.  
  89. private void showNextQuestion() {
  90. rb1.setTextColor(textColorDefaultRb);
  91. rb2.setTextColor(textColorDefaultRb);
  92. rb3.setTextColor(textColorDefaultRb);
  93. rbGroup.clearCheck();
  94.  
  95. if (questionCounter < questionCountTotal) {
  96. currentQuestion = questionList.get(questionCounter);
  97.  
  98. textViewQuestion.setText(currentQuestion.getQuestion());
  99. rb1.setText(currentQuestion.getOption1());
  100. rb2.setText(currentQuestion.getOption2());
  101. rb3.setText(currentQuestion.getOption3());
  102.  
  103. questionCounter++;
  104. textViewQuestionCount.setText("Question: " + questionCounter + "/" + questionCountTotal);
  105. answered = false;
  106. buttonConfirmNext.setText("Confirm");
  107.  
  108. timeLeftInMillis = COUNTDOWN_IN_MILLIS;
  109. startCountDown();
  110. } else {
  111. finishQuiz();
  112. }
  113. }
  114.  
  115. private void restoreQuestion() {
  116. currentQuestion = questionList.get(questionCounter-1);
  117.  
  118. textViewQuestion.setText(currentQuestion.getQuestion());
  119. rb1.setText(currentQuestion.getOption1());
  120. rb2.setText(currentQuestion.getOption2());
  121. rb3.setText(currentQuestion.getOption3());
  122.  
  123. textViewScore.setText("Score: " + score);
  124. textViewQuestionCount.setText("Question: " + questionCounter + "/" + questionCountTotal);
  125.  
  126. if (!answered) {
  127. buttonConfirmNext.setText("Confirm");
  128. startCountDown();
  129. } else {
  130. updateCountDownText();
  131. showSolution();
  132. }
  133. }
  134.  
  135. private void startCountDown() {
  136. countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
  137. @Override
  138. public void onTick(long millisUntilFinished) {
  139. timeLeftInMillis = millisUntilFinished;
  140. updateCountDownText();
  141. }
  142.  
  143. @Override
  144. public void onFinish() {
  145. timeLeftInMillis = 0;
  146. updateCountDownText();
  147. checkAnswer();
  148. }
  149. }.start();
  150. }
  151.  
  152. private void updateCountDownText() {
  153. int minutes = (int) (timeLeftInMillis / 1000) / 60;
  154. int seconds = (int) (timeLeftInMillis / 1000) % 60;
  155.  
  156. String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
  157.  
  158. textViewCountDown.setText(timeFormatted);
  159.  
  160. if (timeLeftInMillis < 10000) {
  161. textViewCountDown.setTextColor(Color.RED);
  162. } else {
  163. textViewCountDown.setTextColor(textColorDefaultCd);
  164. }
  165. }
  166.  
  167. private void checkAnswer() {
  168. answered = true;
  169.  
  170. countDownTimer.cancel();
  171.  
  172. RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
  173. int answerNr = rbGroup.indexOfChild(rbSelected) + 1;
  174.  
  175. if (answerNr == currentQuestion.getAnswerNr()) {
  176. score++;
  177. textViewScore.setText("Score: " + score);
  178. }
  179.  
  180. showSolution();
  181. }
  182.  
  183. private void showSolution() {
  184. rb1.setTextColor(Color.RED);
  185. rb2.setTextColor(Color.RED);
  186. rb3.setTextColor(Color.RED);
  187.  
  188. switch (currentQuestion.getAnswerNr()) {
  189. case 1:
  190. rb1.setTextColor(Color.GREEN);
  191. textViewQuestion.setText("Answer 1 is correct");
  192. break;
  193. case 2:
  194. rb2.setTextColor(Color.GREEN);
  195. textViewQuestion.setText("Answer 2 is correct");
  196. break;
  197. case 3:
  198. rb3.setTextColor(Color.GREEN);
  199. textViewQuestion.setText("Answer 3 is correct");
  200. break;
  201. }
  202.  
  203. if (questionCounter < questionCountTotal) {
  204. buttonConfirmNext.setText("Next");
  205. } else {
  206. buttonConfirmNext.setText("Finish");
  207. }
  208. }
  209.  
  210. private void finishQuiz() {
  211. Intent resultIntent = new Intent();
  212. resultIntent.putExtra(EXTRA_SCORE, score);
  213. setResult(RESULT_OK, resultIntent);
  214. finish();
  215. }
  216.  
  217. @Override
  218. public void onBackPressed() {
  219. if (backPressedTime + 2000 > System.currentTimeMillis()) {
  220. finishQuiz();
  221. } else {
  222. Toast.makeText(this, "Press back again to finish", Toast.LENGTH_SHORT).show();
  223. }
  224.  
  225. backPressedTime = System.currentTimeMillis();
  226. }
  227.  
  228. @Override
  229. protected void onDestroy() {
  230. super.onDestroy();
  231. if (countDownTimer != null) {
  232. countDownTimer.cancel();
  233. }
  234. }
  235.  
  236. @Override
  237. protected void onSaveInstanceState(Bundle outState) {
  238. super.onSaveInstanceState(outState);
  239. outState.putInt(KEY_SCORE, score);
  240. outState.putInt(KEY_QUESTION_COUNT, questionCounter);
  241. outState.putLong(KEY_MILLIS_LEFT, timeLeftInMillis);
  242. outState.putParcelableArrayList(KEY_QUESTION_LIST, questionList);
  243. outState.putBoolean(KEY_ANSWERED, answered);
  244. }
  245. }
Add Comment
Please, Sign In to add comment