Learn to create a Roll Dice Game on Android
A Roll Dice Game is a simple game in which you have to roll some dice and trying to make the highest score possible by adding the values of the two dice. In this tutorial, you are going to learn how to create a Roll Dice Game on Android. It’s a good way to discover how to manipulate simple view and how to get resources dynamically.
Note that you can enjoy this tutorial in video on YouTube :
Get Dice Images
Obviously, we need dice images for our Roll Dice Game. We are going to use these images with values from one to six :
Create the User Interface
The User Interface of our Roll Dice Game will be pretty simple with two image views components to display the dice and a button to let the users to start rolling the dice. It looks like this :
Write the Java Code
The last part of our tutorial consists to write the Java Code of our Main Activity. We start by getting the references of both Image Views and of the Button.
Then, we set an OnClickListener on the Button to write the code letting the dice to be rolled. First, we generate two random values between one and six for each dice. Then, we get the dice image associated to each of these values by using the getIdentifier method of the Resources objet. It lets us to get dynamically the resources identifier of the images drawable.
Last step is to update the image resource value of each image views to display the dice. It gives us the following code :
Play to Roll Dice Game
Finally, we can play to our Roll Dice Game and enjoy a double six. What a luck !
To discover more tutorials on Android Development, don’t hesitate to visit the SSaurel’s Channel : https://www.youtube.com/user/sylsau