Day36 of #100DaysOfCode
3 min readMar 14, 2022
Hii folks 🙌
Today I completed the last few lessons of the same Pathway 4, where I learned to add conditional behavior and image to the app.
Unit 1: Kotlin basics
Pathway 4: Add a button to an app
Source: https://developer.android.com/courses/android-basics-kotlin/course
Add conditional behavior in Kotlin
- We use an
if
statement to set a condition for executing some instructions. For example, if the user rolls the lucky number, print a winning message.
fun main() {
val num = 5
if (num > 4) {
println("The variable is greater than 4")
}
}
- The
Boolean
datatype has values oftrue
andfalse
and can be used for decision-making. - Compare values using operators such as greater than (
>
), less than (<
), and equal to (==
). - We use a chain of
else if
statements to set multiple conditions. For example, print a different message for each possible dice roll. - We use an
else
statement at the end of a chain of conditions to catch any cases that may not be covered explicitly. If we cover the cases for 6-sided dice, anelse
statement would catch the 7 and 8 numbers rolled with 8-sided dice.
fun main() {
val num = 4
if (num > 4) {
println("The variable is greater than 4")
} else if (num == 4) {
println("The variable is equal to 4")
} else {
println("The variable is less than 4")
}
}
- We use a
when
statement as a compact form of executing code based on comparing a value.
when (rollResult) {
luckyNumber -> println("You won!")
1 -> println("So sorry! You rolled a 1. Try again!")
2 -> println("Sadly, you rolled a 2. Try again!")
3 -> println("Unfortunately, you rolled a 3. Try again!")
4 -> println("No luck! You rolled a 4. Try again!")
5 -> println("Don't cry! You rolled a 5. Try again!")
6 -> println("Apologies! you rolled a 6. Try again!")
}
- The general form of if-else:
if (condition-is-true) {
execute-this-code
} else if (condition-is-true) {
execute-this-code
} else {
execute-this-code
}
- When statement:
when (variable) {
matches-value -> execute-this-code
matches-value -> execute-this-code
}
Add images to the Dice Roller app
- We use
setImageResource()
to change the image that's displayed in anImageView
private fun rollDice() {
val dice = Dice(6)
val diceRoll = dice.roll()
val diceImage: ImageView = findViewById(R.id.imageView)
diceImage.setImageResource(R.drawable.dice_2)
}
- We use control flow statements like
if / else
expressions orwhen
expressions to handle different cases in our app, for example, showing different images under different circumstances.
/**
* Roll the dice and update the screen with the result.
*/
private fun rollDice() {
// Create new Dice object with 6 sides and roll the dice
val dice = Dice(6)
val diceRoll = dice.roll() // Find the ImageView in the layout
val diceImage: ImageView = findViewById(R.id.imageView) // Determine which drawable resource ID to use based on the dice roll
val drawableResource = when (diceRoll) {
1 -> R.drawable.dice_1
2 -> R.drawable.dice_2
3 -> R.drawable.dice_3
4 -> R.drawable.dice_4
5 -> R.drawable.dice_5
else -> R.drawable.dice_6
} // Update the ImageView with the correct drawable resource ID
diceImage.setImageResource(drawableResource) // Update the content description
diceImage.contentDescription = diceRoll.toString()
}
That is all for Day36 ✅
Thanks for reading, See you tomorrow!