• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

What is the logic behind updateQuestion regarding the animations in Trivia App?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: cross-posted to stack overflow(https://stackoverflow.com/questions/67352592/how-does-setmaxstreams-in-soundpool-work)--couldn't get an answer there.

I'm creating a Trivia App on Android Studio. For each statement, the user has to select true or false. Based on the input, if the answer they selected is correct, the statement text will fade in green whereas if the user selected the wrong answer, the statement text will shake red. This is my code so far:

   List<Question> questionList;
       private ActivityMainBinding binding;
       private int currentQuestionIndex = 0;
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
   
           questionList = new Repository().getQuestions(questionArrayList -> {
                       binding.questionTextview.setText(questionArrayList.get(currentQuestionIndex)
                               .getAnswer());
   
                       updateCounter(questionArrayList);
                   }
   
           );
   
   
           binding.buttonNext.setOnClickListener(view -> {
   
               currentQuestionIndex = (currentQuestionIndex + 1) % questionList.size();
               updateQuestion();
   
           });
           binding.buttonTrue.setOnClickListener(view -> {
               checkAnswer(true);
               updateQuestion(); //here
   
           });
           binding.buttonFalse.setOnClickListener(view -> {
               checkAnswer(false);
               updateQuestion(); //here
   
           });
   
   
       }
   //checking if the answer is correct, or not.
       private void checkAnswer(boolean userChoseCorrect) {
           boolean answer = questionList.get(currentQuestionIndex).isAnswerTrue();
           int snackMessageId = 0;
           if (userChoseCorrect == answer) {
               snackMessageId = R.string.correct_answer;
               fadeAnimation();
           } else {
               snackMessageId = R.string.incorrect;
               shakeAnimation();
           }
           Snackbar.make(binding.cardView, snackMessageId, Snackbar.LENGTH_SHORT)
                   .show();
   
       }
   //updating the counter. The counter will basically display the question # / total questions
       private void updateCounter(ArrayList<Question> questionArrayList) {
           binding.textViewOutOf.setText(String.format(getString(R.string.text_formatted),
                   currentQuestionIndex, questionArrayList.size()));
       }
   
       private void fadeAnimation() {
           AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
           alphaAnimation.setDuration(300);
           alphaAnimation.setRepeatCount(1);
           alphaAnimation.setRepeatMode(Animation.REVERSE);
   
           binding.cardView.setAnimation(alphaAnimation);
   
           alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
               @Override
               public void onAnimationStart(Animation animation) {
                   binding.questionTextview.setTextColor(Color.GREEN);
               }
   
               @Override
               public void onAnimationEnd(Animation animation) {
                   binding.questionTextview.setTextColor(Color.WHITE);
               }
   
               @Override
               public void onAnimationRepeat(Animation animation) {
   
               }
           });
   
   
       }
   
       private void updateQuestion() {
           String question = questionList.get(currentQuestionIndex).getAnswer();
           binding.questionTextview.setText(question);
           updateCounter((ArrayList<Question>) questionList);
       }
   
       private void shakeAnimation() {
           Animation shake = AnimationUtils.loadAnimation(MainActivity.this,
                   R.anim.shake_animation);
           binding.cardView.setAnimation(shake);
   
           shake.setAnimationListener(new Animation.AnimationListener() {
               @Override
               public void onAnimationStart(Animation animation) {
                   binding.questionTextview.setTextColor(Color.RED);
   
               }
   
               @Override
               public void onAnimationEnd(Animation animation) {
                   binding.questionTextview.setTextColor(Color.WHITE);
               }
   
               @Override
               public void onAnimationRepeat(Animation animation) {
   
               }
           });
   
   
       }

The code here is working perfectly fine. However, before I called updateQuestion() (by accident as a test) in the `onClick` methods of buttonTrue and buttonFalse, the animation was only occurring after I clicked the next button. Why is updateQuestion() method call necessary to call the animations? Why can't I just call checkAnswer() which will call the animation methods?*

Thank you so much!
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic