Skip to content

Commit 275c3c5

Browse files
committed
project #3: clean up the code & add content description
1 parent fcea1e3 commit 275c3c5

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

DiceRoller/app/src/main/java/com/example/diceroller/MainActivity.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class MainActivity : AppCompatActivity() {
1717
val rollButton: Button = findViewById(R.id.button)
1818
// add an event listener for the button
1919
rollButton.setOnClickListener { rollDice() }
20+
21+
rollDice()
2022
}
2123

2224
/*
@@ -28,20 +30,25 @@ Roll the dice and update the screen with the result.
2830
// Create new Dice object with 6 sides and roll it
2931
val dice = Dice(6)
3032
val diceRoll = dice.roll()
31-
// Update the screen with the dice roll images
32-
val diceImage: ImageView = findViewById(R.id.imageView)
3333

34-
// set dice images with numbers
35-
when (diceRoll) {
36-
1 -> diceImage.setImageResource(R.drawable.dice_1)
37-
2 -> diceImage.setImageResource(R.drawable.dice_2)
38-
3 -> diceImage.setImageResource(R.drawable.dice_3)
39-
4 -> diceImage.setImageResource(R.drawable.dice_4)
40-
5 -> diceImage.setImageResource(R.drawable.dice_5)
41-
6 -> diceImage.setImageResource(R.drawable.dice_6)
34+
// Find the ImageView in the layout
35+
val diceImage: ImageView = findViewById(R.id.imageView)
4236

37+
// Determine which drawable resource ID to use based on the dice roll
38+
val drawableResource = when (diceRoll) {
39+
1 -> R.drawable.dice_1
40+
2 -> R.drawable.dice_2
41+
3 -> R.drawable.dice_3
42+
4 -> R.drawable.dice_4
43+
5 -> R.drawable.dice_5
44+
else -> R.drawable.dice_6
4345
}
4446

47+
// Update the ImageView with the correct drawable resource ID
48+
diceImage.setImageResource(drawableResource)
49+
// Update the content description
50+
diceImage.contentDescription = diceRoll.toString()
51+
4552
}
4653
}
4754

0 commit comments

Comments
 (0)