Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.trivia;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.cardview.widget.CardView;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.nfc.cardemulation.CardEmulation;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.view.View;
- import com.example.trivia.data.AnswerListAsyncResponse;
- import com.example.trivia.data.QuestionBank;
- import com.example.trivia.model.Question;
- import com.example.trivia.model.Score;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private ImageButton nextBtn;
- private ImageButton backBtn;
- private Button trueBtn;
- private Button falseBtn;
- private TextView questionTV;
- private TextView questionNumber;
- private TextView scoreTV;
- private TextView topScoreTV;
- private int pointer=0;
- private int points=0;
- private List<Question> questionList;
- private Score score;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- score=new Score();
- SharedPreferences sharedPreferences=this.getSharedPreferences("position",MODE_PRIVATE);
- pointer=sharedPreferences.getInt("position",0);
- //instantiate
- nextBtn=findViewById(R.id.next_button);
- backBtn=findViewById(R.id.back_button);
- trueBtn=findViewById(R.id.true_button);
- falseBtn=findViewById(R.id.false_button);
- questionNumber=findViewById(R.id.question_number);
- questionTV=findViewById(R.id.question_text_view);
- scoreTV=findViewById(R.id.score);
- topScoreTV=findViewById(R.id.topScore);
- falseBtn.setOnClickListener(this);
- trueBtn.setOnClickListener(this);
- nextBtn.setOnClickListener(this);
- backBtn.setOnClickListener(this);
- showHighScore();
- // store questions in a list
- questionList=new QuestionBank().getQuestions(new AnswerListAsyncResponse() {
- @Override
- public void processFinished(ArrayList<Question> questionArrayList) {
- for(int i=0;i<questionArrayList.size();i++){
- Log.d("json data","processFinished"+questionArrayList.get(i).getQtext());}
- updateQuestion();
- updateScore();
- questionNumber();
- }
- });
- }
- @Override
- protected void onPause() {
- super.onPause();
- saveHighScore();
- saveCurrentQuestion();
- }
- @Override
- public void onClick(View view) {
- switch(view.getId()){
- case R.id.true_button:
- Toast.makeText(MainActivity.this,"True",Toast.LENGTH_LONG).show();
- checkAnswer(true);
- updateScore();
- nextQuestion();
- break;
- case R.id.false_button:
- Toast.makeText(MainActivity.this,"False",Toast.LENGTH_LONG).show();
- checkAnswer(false);
- updateScore();
- nextQuestion();
- break;
- case R.id.next_button:
- // Toast.makeText(MainActivity.this,"Next",Toast.LENGTH_LONG).show();
- nextQuestion();
- updateQuestion();
- questionNumber();
- break;
- case R.id.back_button:
- if(pointer<0){
- pointer=913;
- }
- pointer=(pointer-1)%questionList.size();
- updateQuestion();
- questionNumber();
- break;
- }
- }
- //validate answer
- public void checkAnswer(boolean check){
- boolean trueAns=questionList.get(pointer).isAnswerTrue();
- int toastMessageID=0;
- if(check==trueAns){
- toastMessageID=R.string.right_ans;
- trueAnimation();
- increasePoints();
- }else{
- toastMessageID=R.string.wrong_ans;
- falseAnimation();
- decreasePoints();
- }
- Toast.makeText(MainActivity.this,toastMessageID,Toast.LENGTH_LONG).show();
- }
- //increase score
- public void increasePoints(){
- points++;
- score.setScore(points);
- }
- //decrease score
- public void decreasePoints(){
- if(points>0){
- points=points-1;
- }else{
- points=0;
- }
- score.setScore(points);
- }
- // display progression in TextView
- public void questionNumber(){
- int position=pointer+1;
- int size=questionList.size()+1;
- String display= position+"/"+size;
- questionNumber.setText("Question: "+display);
- }
- // display question in TextView
- public void updateQuestion(){
- questionTV.setText(questionList.get(pointer).getQtext());
- }
- // display score in TextView
- public void updateScore(){
- scoreTV.setText(" score:"+score.getScore()+"/914");
- }
- public void saveHighScore(){
- //create sharedPreference file
- SharedPreferences saveHighestScore=getSharedPreferences("high",MODE_PRIVATE);
- //get score from the file
- int highscore=saveHighestScore.getInt("high",0);
- //compare score from xml to score in the class
- if(score.getScore()>highscore){
- SharedPreferences.Editor editor=saveHighestScore.edit();
- editor.putInt("high",score.getScore());
- editor.apply();
- }
- }
- public void showHighScore(){
- SharedPreferences sharedPreferences=getSharedPreferences("high",MODE_PRIVATE);
- int score=sharedPreferences.getInt("high",0);
- topScoreTV.setText("Highest Score:"+Integer.toString(score));
- }
- public void saveCurrentQuestion(){
- //create file
- SharedPreferences lastPosition=getSharedPreferences("position",MODE_PRIVATE);
- SharedPreferences.Editor editor=lastPosition.edit();
- editor.putInt("position",pointer);
- editor.commit();
- }
- /*public void displaySavedQuestion(){
- //create file
- SharedPreferences lastQuestion=getSharedPreferences("pos",MODE_PRIVATE);
- int pos=lastQuestion.getInt("pos",0);
- questionTV.setText(questionList.get(pos).getQtext());
- }*/
- //go to the next question
- public void nextQuestion(){
- pointer=(pointer+1)%questionList.size();
- questionTV.setText(questionList.get(pointer).getQtext());
- }
- //animation
- private void falseAnimation(){
- Animation shake = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shape_animation);
- final CardView cardView=findViewById(R.id.card_view);
- cardView.setAnimation(shake);
- shake.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- cardView.setCardBackgroundColor(Color.RED);
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- cardView.setCardBackgroundColor(Color.WHITE);
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- nextQuestion();
- }
- });
- cardView.startAnimation(shake);
- }
- private void trueAnimation(){
- Animation trueAnim= new AlphaAnimation(1.0f,0.0f);
- trueAnim.setDuration(1500);
- final CardView cardView=findViewById(R.id.card_view);
- trueAnim.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- cardView.setCardBackgroundColor(Color.GREEN);
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- cardView.setCardBackgroundColor(Color.WHITE);
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- nextQuestion();
- }
- });
- cardView.startAnimation(trueAnim);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement