Interaksi Button dengan Aplikasi Dice Roller
Muhammad Ferdian Iqbal/5025201020
PPB I / Github
Pada kesempatan kali ini, saya akan mencoba membuat interaksi pengguna menggunakan button. Apabila button Roll diklik, akan menampilkan dadu dengan nilai yang berbeda.
package com.example.diceroller
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import com.example.diceroller.ui.theme.DiceRollerTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DiceRollerTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
DiceRollerApp()
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun DiceRollerApp(){
DiceRollerWithImageAndButton(
modifier = Modifier
.fillMaxSize()
.wrapContentSize(align = Alignment.Center)
)
}
@Composable
fun DiceRollerWithImageAndButton(modifier: Modifier = Modifier){
var result by remember {
mutableIntStateOf( 1)
}
val imageResource = when(result) {
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
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.wrapContentSize(align = Alignment.Center)
) {
Image(painter = painterResource(id = imageResource), contentDescription = result.toString())
Button(onClick = { result = (1..6).random() }) {
Text(text = "Roll")
}
}
}
Untuk dapat mencobanya, dapat dilakukan clone dahulu repositori Github saya. Terima kasih, sampai bertemu pada kesempatan berikutnya.
Muhammad Ferdian Iqbal/5025201020
PPB I / Github
Comments
Post a Comment