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. Continue reading

Why Your TextView or Button Text Is Not Updating

Understand Why Changing View Text Does Not Work or is Delayed

When changing the text for a TextView using the setText method new Android developers sometimes fail to understand why it does not appear to work. The text not updating also applies to other Views as well, such as the EditView and Button, why this happens is explained. This article is also useful in helping to understand why the User Interface (UI) in your Android App might not be as responsive as expected. The underlying problem is the same. So if you are trying to find out why your setText() on EditText, TextView, Button, etc. is not working as intended, or you UI is sluggish read on, it includes a work around to help you out.

Not All Android Code Executes Immediately

When changing the TextView text this code is second nature to all Android developers:

For those new to programming the above is equivalent to these two lines but saves having to declare the TextView object:

When setText(“New Text”) is run the text is not updated immediately. Android is an event based system. Something happens on the device (the screen is touched, a key is pressed, a call comes in, etc.) and Android raises an event. An App is notified of an event and when one occurs that it needs to respond to it does so, often running the code that you have written. Your App runs its code in a loop under the control of the Android Operating Systems (OS). This code loop is referred to as the App’s thread of execution. There is only one thread and it is responsible for both running the App code and updating the display. The setText call posts a message to update the display, so the update does not happen immediately. Once remaining App code has run the UI messages are processed, then the text changes. A running App’s execution thread can be viewed as shown in this simplified diagram. Continue reading