Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. package com.example.trivia;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.cardview.widget.CardView;
  5.  
  6. import android.content.SharedPreferences;
  7. import android.graphics.Color;
  8. import android.nfc.cardemulation.CardEmulation;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.animation.AlphaAnimation;
  13. import android.view.animation.Animation;
  14. import android.view.animation.AnimationUtils;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.ImageButton;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. import android.view.View;
  21.  
  22. import com.example.trivia.data.AnswerListAsyncResponse;
  23. import com.example.trivia.data.QuestionBank;
  24. import com.example.trivia.model.Question;
  25. import com.example.trivia.model.Score;
  26.  
  27. import java.util.ArrayList;
  28. import java.util.List;
  29.  
  30. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  31.  
  32. private ImageButton nextBtn;
  33. private ImageButton backBtn;
  34. private Button trueBtn;
  35. private Button falseBtn;
  36. private TextView questionTV;
  37. private TextView questionNumber;
  38. private TextView scoreTV;
  39. private TextView topScoreTV;
  40. private int pointer=0;
  41. private int points=0;
  42. private List<Question> questionList;
  43. private Score score;
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47.  
  48.  
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_main);
  51. score=new Score();
  52. SharedPreferences sharedPreferences=this.getSharedPreferences("position",MODE_PRIVATE);
  53. pointer=sharedPreferences.getInt("position",0);
  54.  
  55. //instantiate
  56. nextBtn=findViewById(R.id.next_button);
  57. backBtn=findViewById(R.id.back_button);
  58. trueBtn=findViewById(R.id.true_button);
  59. falseBtn=findViewById(R.id.false_button);
  60. questionNumber=findViewById(R.id.question_number);
  61. questionTV=findViewById(R.id.question_text_view);
  62. scoreTV=findViewById(R.id.score);
  63. topScoreTV=findViewById(R.id.topScore);
  64.  
  65.  
  66. falseBtn.setOnClickListener(this);
  67. trueBtn.setOnClickListener(this);
  68. nextBtn.setOnClickListener(this);
  69. backBtn.setOnClickListener(this);
  70.  
  71. showHighScore();
  72. // store questions in a list
  73. questionList=new QuestionBank().getQuestions(new AnswerListAsyncResponse() {
  74. @Override
  75. public void processFinished(ArrayList<Question> questionArrayList) {
  76. for(int i=0;i<questionArrayList.size();i++){
  77. Log.d("json data","processFinished"+questionArrayList.get(i).getQtext());}
  78.  
  79. updateQuestion();
  80. updateScore();
  81. questionNumber();
  82.  
  83.  
  84. }
  85. });
  86. }
  87.  
  88. @Override
  89. protected void onPause() {
  90. super.onPause();
  91. saveHighScore();
  92. saveCurrentQuestion();
  93.  
  94.  
  95. }
  96.  
  97. @Override
  98. public void onClick(View view) {
  99. switch(view.getId()){
  100.  
  101. case R.id.true_button:
  102. Toast.makeText(MainActivity.this,"True",Toast.LENGTH_LONG).show();
  103. checkAnswer(true);
  104. updateScore();
  105. nextQuestion();
  106.  
  107. break;
  108. case R.id.false_button:
  109. Toast.makeText(MainActivity.this,"False",Toast.LENGTH_LONG).show();
  110. checkAnswer(false);
  111. updateScore();
  112. nextQuestion();
  113. break;
  114.  
  115. case R.id.next_button:
  116. // Toast.makeText(MainActivity.this,"Next",Toast.LENGTH_LONG).show();
  117. nextQuestion();
  118. updateQuestion();
  119. questionNumber();
  120. break;
  121. case R.id.back_button:
  122. if(pointer<0){
  123. pointer=913;
  124.  
  125. }
  126.  
  127. pointer=(pointer-1)%questionList.size();
  128. updateQuestion();
  129. questionNumber();
  130. break;
  131.  
  132. }
  133.  
  134. }
  135.  
  136. //validate answer
  137. public void checkAnswer(boolean check){
  138. boolean trueAns=questionList.get(pointer).isAnswerTrue();
  139. int toastMessageID=0;
  140. if(check==trueAns){
  141. toastMessageID=R.string.right_ans;
  142. trueAnimation();
  143. increasePoints();
  144. }else{
  145. toastMessageID=R.string.wrong_ans;
  146. falseAnimation();
  147. decreasePoints();
  148.  
  149. }
  150.  
  151. Toast.makeText(MainActivity.this,toastMessageID,Toast.LENGTH_LONG).show();
  152.  
  153. }
  154.  
  155. //increase score
  156. public void increasePoints(){
  157. points++;
  158. score.setScore(points);
  159. }
  160.  
  161. //decrease score
  162. public void decreasePoints(){
  163.  
  164. if(points>0){
  165. points=points-1;
  166. }else{
  167. points=0;
  168. }
  169. score.setScore(points);
  170. }
  171.  
  172. // display progression in TextView
  173. public void questionNumber(){
  174. int position=pointer+1;
  175. int size=questionList.size()+1;
  176. String display= position+"/"+size;
  177. questionNumber.setText("Question: "+display);
  178.  
  179. }
  180.  
  181. // display question in TextView
  182. public void updateQuestion(){
  183.  
  184. questionTV.setText(questionList.get(pointer).getQtext());
  185. }
  186.  
  187. // display score in TextView
  188. public void updateScore(){
  189.  
  190. scoreTV.setText(" score:"+score.getScore()+"/914");
  191. }
  192.  
  193. public void saveHighScore(){
  194. //create sharedPreference file
  195. SharedPreferences saveHighestScore=getSharedPreferences("high",MODE_PRIVATE);
  196. //get score from the file
  197. int highscore=saveHighestScore.getInt("high",0);
  198.  
  199. //compare score from xml to score in the class
  200. if(score.getScore()>highscore){
  201. SharedPreferences.Editor editor=saveHighestScore.edit();
  202. editor.putInt("high",score.getScore());
  203. editor.apply();
  204. }
  205. }
  206.  
  207. public void showHighScore(){
  208. SharedPreferences sharedPreferences=getSharedPreferences("high",MODE_PRIVATE);
  209. int score=sharedPreferences.getInt("high",0);
  210. topScoreTV.setText("Highest Score:"+Integer.toString(score));
  211.  
  212. }
  213.  
  214.  
  215. public void saveCurrentQuestion(){
  216. //create file
  217. SharedPreferences lastPosition=getSharedPreferences("position",MODE_PRIVATE);
  218. SharedPreferences.Editor editor=lastPosition.edit();
  219. editor.putInt("position",pointer);
  220. editor.commit();
  221.  
  222. }
  223.  
  224. /*public void displaySavedQuestion(){
  225. //create file
  226. SharedPreferences lastQuestion=getSharedPreferences("pos",MODE_PRIVATE);
  227. int pos=lastQuestion.getInt("pos",0);
  228. questionTV.setText(questionList.get(pos).getQtext());
  229.  
  230. }*/
  231.  
  232. //go to the next question
  233. public void nextQuestion(){
  234. pointer=(pointer+1)%questionList.size();
  235. questionTV.setText(questionList.get(pointer).getQtext());
  236. }
  237.  
  238. //animation
  239. private void falseAnimation(){
  240. Animation shake = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shape_animation);
  241. final CardView cardView=findViewById(R.id.card_view);
  242. cardView.setAnimation(shake);
  243. shake.setAnimationListener(new Animation.AnimationListener() {
  244. @Override
  245. public void onAnimationStart(Animation animation) {
  246. cardView.setCardBackgroundColor(Color.RED);
  247.  
  248. }
  249.  
  250. @Override
  251. public void onAnimationEnd(Animation animation) {
  252. cardView.setCardBackgroundColor(Color.WHITE);
  253.  
  254.  
  255. }
  256.  
  257. @Override
  258. public void onAnimationRepeat(Animation animation) {
  259. nextQuestion();
  260.  
  261. }
  262. });
  263.  
  264. cardView.startAnimation(shake);
  265.  
  266. }
  267.  
  268.  
  269.  
  270. private void trueAnimation(){
  271. Animation trueAnim= new AlphaAnimation(1.0f,0.0f);
  272. trueAnim.setDuration(1500);
  273. final CardView cardView=findViewById(R.id.card_view);
  274. trueAnim.setAnimationListener(new Animation.AnimationListener() {
  275. @Override
  276. public void onAnimationStart(Animation animation) {
  277. cardView.setCardBackgroundColor(Color.GREEN);
  278. }
  279.  
  280. @Override
  281. public void onAnimationEnd(Animation animation) {
  282. cardView.setCardBackgroundColor(Color.WHITE);
  283. }
  284.  
  285. @Override
  286. public void onAnimationRepeat(Animation animation) {
  287. nextQuestion();
  288.  
  289. }
  290. });
  291.  
  292. cardView.startAnimation(trueAnim);
  293. }
  294.  
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement