Android Dice Application
Have you ever wanted to play a board game, and then realized the dice were missing? Or have you ever wanted to learn how to create an Android app? These instructions will teach you how to develop a simple application which will simulate a dice roll. With this app, you can get a random number within a range chosen by you, without having to connect to the internet.
The whole process should take you less than 20 minutes. In order to complete it, you will need:
- Android Studio installed in your computer
- An Android device
- Basic knowledge of Java programming (suggested but not required)
Create an Android Studio Project
- Open up Android Studio.
- Click on "Start a new Android Studio project."
Configure Your Project
- On the "Application name" field, name your app. We will call ours "Dice."
- On "Company Domain", choose the folder hierarchy. We will name it "myexample."
- Press "Next."
Select the Target Devices
- Choose "API 21: Android 5.0 (Lollipop)" as the Minimum SDK.
- This means that any Android device with the Lollipop version (5.0) will be able to run your application. According to Android Studio, 40.5% of the devices active on the Google Play Store will be able to download and run your app.
- Press "Next."
Choose a Starting Activity
- Choose "Empty Activity."
- An "Activity" is how Android defines a screen on an application. Therefore, by choosing an Empty Activity, we are selecting a blank canvas in which we can insert whatever we want.
- Press "Next."
- Press "Finish."
By following these steps, you will have created the project.
Navigate to Your .xml File
If every step was followed correctly, this is what your project should look like.
- On the left, where there is a list of folders, make sure that the project tab is selected.
- Then, navigate to the "activity_main.xml."
- It will be under Dice > app > src > main > res > layout.
- Double click on it to open the file.
Set Up Your App's Visual Interface
On this step you will add widgets, such as buttons or texts to your application. As you might have noticed, this file looks somewhat different from a Java class. This is because Android applications' GUI (Graphical User Interface) relies on xml programming. Because not every Android developer is familiar with xml, Android Studio provides a simple drag-and-drop option, which is what we are going to use.
- With the Design tab selected, click on the "Hello World" widget, which is on the top left corner of the phone, then hit the "delete" key on your keyboard.
- From the left list, called "Palette", drag and drop a "Button" into the screen.
- On the bottom right list, called "Properties", find the attribute "text", and change it to "ROLL THE DIE."
- Note that in the same list, the attribute "id" is set to "button."
- The "ids" are important because they are used to reference the widget from the Java code, which will be done in the next step.
- From the left list, drag and drop a "Large Text" into the screen.
- On the right, find the attribute "text", and change it to "Press the button below."
- Position the button and the text according to your preferences. We placed them centered on the screen.
Connecting Your Widgets to the Code
On this step, you will connect, or "wire" the widgets to your program, using the previously mentioned ids.
- On the left, where the list of folders is, navigate to the MainActivity Java class.
- It is located under Dice > app > src > main > java > myexample.dice.
- Inside the onCreate method, add the following lines under "setContentView(R.layout.activity_main);"
Button button = (Button) findViewById(R.id.button);<br>final TextView text = (TextView) findViewById(R.id.textView); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int min = ; int max = ; int random = ThreadLocalRandom.current().nextInt(min,max+1); text.setText(new StringBuilder().append(random)); } });
- After you paste the lines, Android Studio will display a message asking whether you would like to import new classes into your program. This is because we are referencing some classes (such as Button, ThreadLocalRandom) that are not included in the project, so Android Studio is asking if it can access them in order to know how to proceed when it encounters these references.
3. On the following line, change the value of "min" and "max" according to what range of numbers you want..
int min = ;
int max = ;
For one normal die, it should look like this:
int min = 1; int max = 6;
Finishing Up Your App
This is what your app should look like after you have done the previous step.
Congratulations! You now have a fully functional Android application that simulates a die roll. Now, whenever you need a random number generator, you can open your app on your device and press "ROLL THE DIE."
Connecting Your Android Device
Now that you have completed the programming part of the project, it is time to connect your device to the computer and install the application on it.
- Connect your Android device to your computer through the USB port.
- Run the app by pressing Shift + F10, or clicking the green Play button, located on the top bar
It should look somewhat to the image. If your device does not appear under "Connected Devices", refer to Google on how to enable USB debugging on Android devices.
3. Once you select your device, click "OK."
Running the Application
It could take a couple of minutes to install the application on your device. Once it finishes installing, the application should be opened automatically.
Congratulations! You have a fully functional Android application that simulates a dice roll. Now, when you need a random number generator, you can open the app on your device and press "ROLL THE DIE." Also, if you ever want to change the range of numbers, all you need to do is refer to step 7 and change the values of "min" and "max."