Book Image

Learning Java by Building Android Games

By : John Horton
Book Image

Learning Java by Building Android Games

By: John Horton

Overview of this book

Table of Contents (17 chapters)
Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Math game – getting and checking the answer


Here we will detect the right or wrong answer and provide a pop-up message to the player. Our Java is getting quite good now, so let's dive in and add these features. I will explain things as we go and then, as usual, dissect the code thoroughly at the end.

The already completed code is in the download bundle, in the following files that correspond to the filenames we will create/autogenerate in Android Studio in a moment:

  • Chapter3/MathGameChapter3b/java/MainActivity.java

  • Chapter3/MathGameChapter3b/java/GameActivity.java

  • Chapter3/MathGameChapter3b/layout/activity_main.xml

  • Chapter3/MathGameChapter3b/layout/activity_game.xml

As usual, I recommend following this tutorial step by step to see how we can create all of the code for ourselves.

  1. Open the GameActivity.java file visible in the editor window.

  2. Now we need to add the click detection functionality to our GameActivity, just as we did for our MainActivity. However, we will go a little further than the last...