Add images to the Dice Roller app

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

Add images to the 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

  • Use setImageResource() to change the image that’s displayed in an ImageView
  • Use control flow statements like if / else expressions or when expressions to handle different cases in your app, for example, showing different images under different circumstances.

 

Learn more

 

Practice on your own

Do the following:

  1. Add another dice to the app, so that one Roll button gives 2 dice results. How many ImageViews will you need in your layout? How will that affect the MainActivity.kt code?

Check your work:

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

 

 

 

Leave a Reply

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