Skip to content

Commit

Permalink
Add dice visual
Browse files Browse the repository at this point in the history
  • Loading branch information
nka-coder committed Apr 17, 2021
1 parent e73335b commit 90ced75
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 28 additions & 5 deletions app/src/main/java/com/example/rolldice/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,47 @@ package com.example.rolldice
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import kotlin.random.Random


class MainActivity : AppCompatActivity() {

lateinit var diceImage: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.setOnClickListener {

rollDice()

Toast.makeText(this, "button clicked", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "The dice is rolled", Toast.LENGTH_SHORT).show()
}

diceImage = findViewById(R.id.dice_image)
}

private fun rollDice() {
val resultText: TextView = findViewById(R.id.result_text)
resultText.text = "10"
val randomInt = Random.nextInt(1, 6)

if (randomInt == 1) {
diceImage.setImageResource(R.drawable.dice_1)
}
if (randomInt == 2) {
diceImage.setImageResource(R.drawable.dice_2)
}
if (randomInt == 3) {
diceImage.setImageResource(R.drawable.dice_3)
}
if (randomInt == 4) {
diceImage.setImageResource(R.drawable.dice_4)
}
if (randomInt == 5) {
diceImage.setImageResource(R.drawable.dice_5)
}
if (randomInt == 6) {
diceImage.setImageResource(R.drawable.dice_6)
}
}
}
7 changes: 3 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
android:layout_gravity="center_vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/result_text"
<ImageView
android:id="@+id/dice_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="1"
android:textSize="30sp" />
android:src="@drawable/empty_dice" />

<Button
android:id="@+id/roll_button"
Expand Down

0 comments on commit 90ced75

Please sign in to comment.