loading

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)

Step 1: Create an Android Studio Project

  1. Open up Android Studio.
  2. Click on "Start a new Android Studio project."

Step 2: Configure Your Project

  1. On the "Application name" field, name your app. We will call ours "Dice."
  2. On "Company Domain", choose the folder hierarchy. We will name it "myexample."
  3. Press "Next."

Step 3: Select the Target Devices

  1. 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.
  2. Press "Next."

Step 4: Choose a Starting Activity

  1. 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.
  2. Press "Next."
  3. Press "Finish."

By following these steps, you will have created the project.

Step 5: Navigate to Your .xml File

If every step was followed correctly, this is what your project should look like.

  1. On the left, where there is a list of folders, make sure that the project tab is selected.
  2. Then, navigate to the "activity_main.xml."
    • It will be under Dice > app > src > main > res > layout.
  3. Double click on it to open the file.

Step 6: 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.

  1. 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.
  2. From the left list, called "Palette", drag and drop a "Button" into the screen.
  3. On the bottom right list, called "Properties", find the attribute "text", and change it to "ROLL THE DIE."
  4. 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.
  5. From the left list, drag and drop a "Large Text" into the screen.
  6. On the right, find the attribute "text", and change it to "Press the button below."
  7. Position the button and the text according to your preferences. We placed them centered on the screen.

Step 7: Connecting Your Widgets to the Code

On this step, you will connect, or "wire" the widgets to your program, using the previously mentioned ids.

  1. 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.
  2. 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;

Step 8: 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."

    Step 9: 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.

    1. Connect your Android device to your computer through the USB port.
    2. 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."

    Step 10: 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."

    <p>How to use this app?</p>
    <p>Nice app! Did something like this for school, but we had images of each side of the die and had it display them instead of just text. :)</p>
    <p>Omg! amazing :) </p>

    About This Instructable

    2,837views

    53favorites

    License:

    More by VictorL117:Android Dice Application 
    Add instructable to: