Skip to content

Commit fcea1e3

Browse files
committed
project #3: add dice images with it's own number
Replace text view with Image view for the dice
1 parent 50140f3 commit fcea1e3

File tree

8 files changed

+26
-16
lines changed

8 files changed

+26
-16
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.example.diceroller
22

33
import android.os.Bundle
44
import android.widget.Button
5-
import android.widget.TextView
5+
import android.widget.ImageView
66
import androidx.appcompat.app.AppCompatActivity
77

88
/**
@@ -28,9 +28,19 @@ Roll the dice and update the screen with the result.
2828
// Create new Dice object with 6 sides and roll it
2929
val dice = Dice(6)
3030
val diceRoll = dice.roll()
31-
// Update the screen with the dice roll
32-
val resultTextView: TextView = findViewById(R.id.textView)
33-
resultTextView.text = diceRoll.toString()
31+
// Update the screen with the dice roll images
32+
val diceImage: ImageView = findViewById(R.id.imageView)
33+
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)
42+
43+
}
3444

3545
}
3646
}
20.2 KB
Loading
19.8 KB
Loading
20 KB
Loading
20.7 KB
Loading
21.8 KB
Loading
22.9 KB
Loading

DiceRoller/app/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
10-
android:id="@+id/textView"
11-
android:layout_width="wrap_content"
12-
android:layout_height="wrap_content"
13-
android:textSize="36sp"
14-
app:layout_constraintBottom_toBottomOf="parent"
15-
app:layout_constraintLeft_toLeftOf="parent"
16-
app:layout_constraintRight_toRightOf="parent"
17-
app:layout_constraintTop_toTopOf="parent"
18-
tools:text="1" />
19-
209
<Button
2110
android:id="@+id/button"
2211
android:layout_width="wrap_content"
2312
android:layout_height="wrap_content"
13+
android:layout_marginTop="16dp"
2414
android:text="@string/roll"
2515
app:layout_constraintEnd_toEndOf="parent"
2616
app:layout_constraintStart_toStartOf="parent"
27-
app:layout_constraintTop_toBottomOf="@+id/textView" />
17+
app:layout_constraintTop_toBottomOf="@+id/imageView" />
18+
19+
<ImageView
20+
android:id="@+id/imageView"
21+
android:layout_width="160dp"
22+
android:layout_height="200dp"
23+
app:layout_constraintBottom_toBottomOf="parent"
24+
app:layout_constraintEnd_toEndOf="parent"
25+
app:layout_constraintStart_toStartOf="parent"
26+
app:layout_constraintTop_toTopOf="parent"
27+
tools:srcCompat="@drawable/dice_1" />
2828

2929
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)