Tugas 4 PPB I
Tugas 4 - Membuat Aplikasi Sederhana Acak Dadu dengan Menggunakan Android Studio
Nama: Muhammad Ghani Taufiqurrahman Atmaja
NRP : 5025201110
Kelas: PPB - I
Pada tugas 4 ini, Saya akan membuat aplikasi android sederhana untuk melakukan lempar dadu secara random dengan menggunakan Jetpack Compose melalui Android Studio. Dalam pembuatan aplikasi ini, saya mengambil referensi dari website developer.android.com untuk pengerjaan tugas ini. Berikut hasil dan source code tugas ini:
Source Code: Repo
Source Code: Repo
Code:
package com.example.tugas4ppb
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.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.remember
import androidx.compose.material3.Text
import androidx.compose.material3.Button
import androidx.compose.runtime.Composable
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.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.example.tugas4ppb.ui.theme.Tugas4PPBTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Tugas4PPBTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
DiceRollerApp()
}
}
}
}
}
@Preview
@Composable
fun DiceRollerApp(){
DiceWithButtonAndImage(modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center))
}
@Composable
fun DiceWithButtonAndImage(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(Alignment.Center)
) {
Image(painter = painterResource(id = imageResource),
contentDescription = result.toString())
Button(onClick = { result = (1..6).random() }){
Text(stringResource(R.string.roll))
}
}
}
Hasil:
Comments
Post a Comment