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
- Click on the Solution Code URL. This opens the GitHub page for the project in a browser.
- On the GitHub page for the project, click the Clone or download button, which brings up a Clone dialog.
- In the dialog, click the Download ZIP button to save the project to your computer. Wait for the download to complete.
- Locate the file on your computer (likely in the Downloads folder).
- Double-click the ZIP file to unpack it. This creates a new folder that contains the project files.
Open the project in Android Studio
- Start Android Studio.
- In the Welcome to Android Studio window, click Open an existing Android Studio project.
Note: If Android Studio is already open, instead, select the File > New > Import Project menu option.
- In the Import Project dialog, navigate to where the unzipped project folder is located (likely in your Downloads folder).
- Double-click on that project folder.
- Wait for Android Studio to open the project.
- Click the Run button
to build and run the app. Make sure it works as expected.
- 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 anImageView
- Use control flow statements like
if / else
expressions orwhen
expressions to handle different cases in your app, for example, showing different images under different circumstances.
Learn more
- Android Kotlin glossary
if
expression in Kotlinwhen
expression in KotlinImageView.setImageResource()
- Accessibility
- https://developer.android.com/codelabs/basic-android-kotlin-training-conditional-dice-roll-behavior#7
Practice on your own
Do the following:
- 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 theMainActivity.kt
code?
Check your work:
Your finished app should run without errors and show the two dice.