DICE ROLLER using android studio

In this project ready tutorial we will discuss how to create a DICE ROLLER using android studio It contains : Creating the Project and Adding an Imageview Widget .

We‟ll develop a simple dice rolling app in this chapter. We‟ll learn how to use images in the GUI and also code for basic random number generation in Java for rolling a virtual dice. When we hit a Roll button, the app will choose a number between 1 and 6 randomly, show the result as a number in a TextView and also display a dice image that shows the outcome.

Please create a new project and save it on your computer. Select an empty activity as usual. I named my project as Dice Roller but you can of course give any name you‟d like

While the activity_main.xml file is opened in Android Studio, please change the default Textview‟s text from Hello World to Dice Roller and position it on the top of the GUI aligned horizontally in the middle as shown in Figure

Please select and right-click  copy all of these images in the file explorer of your computer (just as you do when you select files for copy-paste) and then right-click  paste in the drawable folder in Android Studio as shown below:

Save the image as shown below according to their name

When we click the arrow symbol just at the left of drawable folder, we can see the newly added image files as in Figure

Let‟s place the ImageView object to the GUI now. Please find the ImageView object in the Palette and drag and drop to the app‟s layout as follows:

When we drop the ImageView on the GUI, Android Studio wants us to set its image as in Figure . The selected image will be the image shown inside the ImageView when the app first starts (i.e. default image). Therefore, please select the dicegeneral.png as in Figure.

After this selection, press OK and then the layout of our app will be shown as follows:

Adding the Other Widgets and Setting up the IDs

Please place a TextView and a Button just below the ImageView object which will display the result of the rolling and initiate the rolling, respectively. I have set the TextView to show Please tap the button…. Similarly, the button‟s text is changed to ROLL! as shown below:

We‟ll need the IDs of the dice image, the result TextView and the button because we‟ll access them in the code. I‟ve assigned their IDs as diceImage, rollResult and rollButton, respectively.

Developing the Main Code of the App

Building and Running the App

Run the Dice Roller on the android emulator or our physical and enjoy the game .

Learn to create BMI Calculator in Android Studio

Learn to create Red Blue light app using Android studio

Leave a Reply

Your email address will not be published.