TUGAS 4 - Pemrograman Perangkat Bergerak

 Nama   : Theresia Nawangsih
 NRP       : 5025201144
 Kelas    : PPB - I

Komponen Button

Pada pertemuan kali ini, kelas kami diminta untuk membuat project " Dice Roller"

Pertama, pilih new project lalu memilih 'Empty Activity'


Lalu yang terakhir, sesuaikan code dengan kebutuhan. Jika saya seperti video dibawah ini






Source Code
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.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
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.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.diceroller.ui.theme.DiceRollerTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DiceRollerTheme {
// A surface container using the 'background' color from the theme
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 { mutableStateOf( 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(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
Image(
painter = painterResource(imageResource),
contentDescription = result.toString()
)

Text(
text = "Result: $result", // Display the dice number result
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(vertical = 8.dp)
)

Button(
onClick = { result = (1..6).random() },
modifier = Modifier
.padding(16.dp)
.width(200.dp)
.height(50.dp)
) {
Text(text = stringResource(R.string.acak), fontSize = 24.sp)
}
}
}

Strings.xml

<resources>
<string name="app_name">Dice Roller</string>
<string name="acak">Acak</string>
</resources>

Comments

Popular posts from this blog

Tugas 3 - Pemrograman Perangkat Bergerak

TUGAS 2 - Pemrograman Perangkat Bergerak

TUGAS 1 - Pemrograman Perangkat Bergerak