Tugas Minggu 6 - PPB I
Nama : Tegar Ganang Satrio Priambodo
NRP : 5025201002
Kelas : PPB I
Memahami Bahasa Kotlin untuk Pengembangan Aplikasi Android
Pada pertemuan kali ini, saya akan membuat project kalkulator sederhana dengan bahasa Kotlin. Cara kerja kalkulator ini adalah pengguna dapat mengisi angka pada dua kolom. Kemudian pengguna bisa memilih operasi apa saja yang diinginkan. Hasil akan muncul berupa allert yang menunjukkan hasil kalkulasi tiap operasi yang di interaksikan.
berikut adalah code pengerjaan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mycalculator | |
import android.os.Bundle | |
import android.widget.Toast | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.Spacer | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
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.Modifier | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.unit.dp | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
SimpleCalculator() | |
} | |
} | |
} | |
@Composable | |
fun SimpleCalculator() { | |
var num1 by remember { | |
mutableStateOf("0") | |
} | |
var num2 by remember { | |
mutableStateOf("0") | |
} | |
val context = LocalContext.current | |
Column { | |
TextField( | |
value = num1, | |
onValueChange = { | |
num1 = it | |
}, | |
modifier = Modifier.fillMaxWidth() | |
) | |
TextField( | |
value = num2, | |
onValueChange = { | |
num2 = it | |
}, | |
modifier = Modifier.fillMaxWidth() | |
) | |
Row( | |
modifier = Modifier | |
.fillMaxWidth(), | |
horizontalArrangement = Arrangement.SpaceBetween | |
) { | |
Button(onClick = { | |
val result = num1.toInt() + num2.toInt() | |
Toast.makeText(context, "Result for $num1 + $num2 is $result", | |
Toast.LENGTH_SHORT | |
).show() | |
}) { | |
Text(text = "Add") | |
} | |
Spacer(modifier = Modifier.width(20.dp)) | |
Button(onClick = { | |
val result = num1.toInt() - num2.toInt() | |
Toast.makeText(context, "Result for $num1 - $num2 is $result", | |
Toast.LENGTH_SHORT | |
).show() | |
}) { | |
Text(text = "Sub") | |
} | |
Spacer(modifier = Modifier.width(20.dp)) | |
Button(onClick = { | |
val result = num1.toInt() * num2.toInt() | |
Toast.makeText(context, "Result for $num1 * $num2 is $result", | |
Toast.LENGTH_SHORT | |
).show() | |
}) { | |
Text(text = "Mul") | |
} | |
Spacer(modifier = Modifier.width(20.dp)) | |
Button(onClick = { | |
val result = num1.toInt() / num2.toInt() | |
Toast.makeText(context, "Result for $num1 / $num2 is $result", | |
Toast.LENGTH_SHORT | |
).show() | |
}) { | |
Text(text = "Div") | |
} | |
} | |
} | |
} |
dan berikut adalah video demo program
]
Komentar
Posting Komentar