Dice Roller Source Code for Android Apps

Free to Use Android Dice Roller Source Code to Add to Your App

An Android Dice Roller tutorial with source code for a basic dice roller App for Android. Simply add the example source code to any App that requires a dice roll. Dice face images and a 3D dice icon image provided, all images and code is free to use and royalty free. The code here is for a six sided dice but can be adapted for larger dice or more than one dice. There are no restrictions placed upon the code or the dice face images (all the images are free as they are in the public domain). To try out this Android dice roller source code start by generating a new App in Eclipse, we simply called it Dice. (If you are new to Android programming see the articles Set Up Windows for Android Development and Your First Android Hello World Java Program.)

3D Dice Icon for Android Dice Roller Source Code

(BTW we know that dice is the plural and means two or more die, but it is common to refer to one die as ‘a dice’ so we will be doing that here.)

Add the Dice Face Images

The code given in this article is for a common six sided dice, a cube, and therefore each side of the cube, each face, has a number from one to six, we will use six PNG images to show the dice roll result, plus a 3D image when rolling, the same 3D image is used for the App icon. These images were by the Open Clip Art Library user rg1024.

1st Dice Face for the Android Dice Roller Source Code2nd Dice Face for the Android Dice Roller Source Code3rd Dice Face for the Android Dice Roller Source Code4th Dice Face for the Android Dice Roller Source Code5th Dice Face for the Android Dice Roller Source Code6th Dice Face for the Android Dice Roller Source Code3D Dice for the Android Dice Roller Source Code

The sound of a dice roll will be used, by Mike Koenig and is from SoundBilble.com.

All the resources ready to import into the Dice project are available from this dice resources Zip file. Once imported (select Archive File under General from the File menu, then Import) change the App icon entry in the AndroidManifest.xml file to dice3d, i.e. android:icon=”@drawable/dice3d”. With the visuals and audio done we can move on to the actual Android dice roller source code proper.

Android Dice Roller Source Code

Open the activity_main.xml layout file and add an ImageView (in the Graphical Layout editor the ImageView is under the Images & Media folder on the Palette bar, drag and drop one onto the layout and select dice3droll on the Resource Chooser dialog).

A Java Random class (to generate the dice numbers) is declared along with an Android SoundPool (to play the dice roll sound). To load the right picture for each roll a switch statement is used. To provide some feedback to the user so that they can see that a new roll has occurred (in case identical numbers are rolled in succession), the 3D dice image will be displayed;.but because the roll happens so fast (unlike a real dice) the image would not be seen, so a Timer is used to provide a delay (allowing the UI to update,). A Handler message signals to a Callback to perform the roll (the same method as described in the post Why Your TextView or Button Text Is Not Updating). Finally the roll value is used to update the UI. Here is the code:

Imports will be required for ImageView, Random, SoundPoolAudioManager, Timer, TimerTaskViewHandlerCallback, Message. An aid to adding the imports in Eclipse is to hover the mouse over the class type with the red underline and select the appropriate Import statement from the list that appears, e.g. for Handler it is Import ‘Handler’ (android.os).

For completeness the layout used in this example is simple enough:

Android Dice Roller Source Code RunningThe Android dice roller source code is now ready to run, either in an AVD or an actual Android device. (If running the code on an AVD and the sound of the dice roll is not heard see the article Enable Sound in the Android Virtual Device).

The complete source code is available as a Zip file from the Android Example Projects page or by selecting android_dice_roller.zip. The code can be seen running using Dice.apk, also linked to on the projects page.


The dice images were from the Open Clip Art Library by user rg1024. As well as all the resources being available in the Zip file mentioned in the article the 3D dice icons are available in a separate Zip file for use as icons in other Apps, see the Android Graphic Resources page. The dice roll sound is from SoundBible.com.

3 thoughts on “Dice Roller Source Code for Android Apps

  1. I dont get it.
    I have tried and tried, but I don
    t get it.

    Line: handler=new Handler(callback);
    show me an error: <>

    If I quit \callback\ execution doesn`t enter on handleMessage(Message msg)

    If I initialize new Handler(callback); with others ideas the app crashes.

    Can you help me, please?
    Thanks a lot!

    • Hi,

      the Handler class used here is from android.os and not java.util.logging, Callback is from android.os as well, though you probably used the correct imports. To help I’ve added a zip file with all the source code to the Android Example Projects page as well as a link in this article. You can import the code from the Zip file into a new Android project and run it to see it working. Failing that there is an APK file on the same page to install onto a device to see the code working (link to APK in article as well).

      Note you may need to change your device settings temporarily to allow the APK to be installed, only install APKs from trusted sources.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.