Create an interactive Dice Roller app

Last Updated on December 20, 2020 by AbdurRahman G Official | Md Ghufran Salafi

Create an interactive Dice Roller app:

 

Solution code

The solution code for this codelab is in the project and module shown below.

To get the code for this codelab and open it in Android Studio, do the following.

Get the code

  1. Click on the Solution Code URL. This opens the GitHub page for the project in a browser.
  2. On the GitHub page for the project, click the Clone or download button, which brings up a Clone dialog.

v5ZBt4SZytjyBnVvpTogKBvrpekuBZpgU-HyrX7i5PZYupqIBs5ZRUR8iyHJODQJpugZRtz-zLi5-O1nGiEvdL4VTGyvT_W3MINzRBsUa25zeJdt9tol7-msOoT8AJ-RWMl81wsR_w

  1. In the dialog, click the Download ZIP button to save the project to your computer. Wait for the download to complete.
  2. Locate the file on your computer (likely in the Downloads folder).
  3. Double-click the ZIP file to unpack it. This creates a new folder that contains the project files.

Open the project in Android Studio

  1. Start Android Studio.
  2. In the Welcome to Android Studio window, click Open an existing Android Studio project.

Tdjf5eS2nCikM9KdHgFaZNSbIUCzKXP6WfEaKVE2Oz1XIGZhgTJYlaNtXTHPFU1xC9pPiaD-XOPdIxVxwZAK8onA7eJyCXz2Km24B_8rpEVI_Po5qlcMNN8s4Tkt6kHEXdLQTDW7mg

Note: If Android Studio is already open, instead, select the File > New > Import Project menu option.

PaMkVnfCxQqSNB1LxPpC6C6cuVCAc8jWNZCqy5tDVA6IO3NE2fqrfJ6p6ggGpk7jd27ybXaWU7rGNOFi6CvtMyHtWdhNzdAHmndzvEdwshF_SG24Le01z7925JsFa47qa-Q19t3RxQ

  1. In the Import Project dialog, navigate to where the unzipped project folder is located (likely in your Downloads folder).
  2. Double-click on that project folder.
  3. Wait for Android Studio to open the project.
  4. Click the Run button j7ptomO2PEQNe8jFt4nKCOw_Oc_Aucgf4l_La8fGLCMLy0t9RN9SkmBFGOFjkEzlX4ce2w2NWq4J30sDaxEe4MaSNuJPpMgHxnsRYoBtIV3-GUpYYcIvRJ2HrqR27XGuTS4F7lKCzg to build and run the app. Make sure it works as expected.
  5. Browse the project files in the Project tool window to see how the app was implemented.

 

Summary

  • Add a Button in an Android app using the Layout Editor.
  • Modify the MainActivity.kt class to add interactive behavior to the app.
  • Pop up a Toast message as a temporary solution to verify you’re on the right track.
  • Set an on-click listener for a Button using setOnClickListener() to add behavior for when a Button is clicked.
  • When the app is running, you can update the screen by calling methods on the TextViewButton, or other UI elements in the layout.
  • Comment your code to help other people who are reading your code understand what your approach was.
  • Reformat your code and clean up your code.

 

Learn more

 

Practice on your own

Do the following:

  1. Add another dice to the app. Clicking on the Roll button should roll 2 dice. The results should be displayed in 2 different TextViews on screen.

Check your work:

Your finished app should run without errors and show two dice in the app.

 

 

 

Leave a Reply

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