Skip to content

Instantly share code, notes, and snippets.

@florentinobenedictus
Created April 17, 2024 09:16
Show Gist options
  • Save florentinobenedictus/bdb535818a8b8984b395e0f12de24bea to your computer and use it in GitHub Desktop.
Save florentinobenedictus/bdb535818a8b8984b395e0f12de24bea to your computer and use it in GitHub Desktop.
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.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.mycalculator.ui.theme.MyCalculatorTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CalculatorScreen()
}
}
}
@Composable
fun CalculatorScreen() {
var num1 by remember {
mutableStateOf("0")
}
var num2 by remember {
mutableStateOf("0")
}
val context = LocalContext.current
Column {
TextField(value = num1, onValueChange = {
num1 = it
})
TextField(value = num2, onValueChange = {
num2 = it
})
Row {
Button(onClick = {
var result = num1.toInt() + num2.toInt()
Toast.makeText(context, "Result of $num1 + $num2 is $result", Toast.LENGTH_SHORT).show()
}) {
Text(text = "Add")
}
Spacer(modifier = Modifier.width(16.dp))
Button(onClick = {
var result = num1.toInt() - num2.toInt()
Toast.makeText(context, "Result of $num1 - $num2 is $result", Toast.LENGTH_SHORT).show()
}) {
Text(text = "Sub")
}
Spacer(modifier = Modifier.width(16.dp))
Button(onClick = {
var result = num1.toInt() * num2.toInt()
Toast.makeText(context, "Result of $num1 * $num2 is $result", Toast.LENGTH_SHORT).show()
}) {
Text(text = "Mul")
}
Spacer(modifier = Modifier.width(16.dp))
Button(onClick = {
var result = num1.toInt() / num2.toInt()
Toast.makeText(context, "Result of $num1 / $num2 is $result", Toast.LENGTH_SHORT).show()
}) {
Text(text = "Div")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment