package com.example.tipcalculatorkt import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button import androidx.compose.material3.Scaffold 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.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.times import com.example.tipcalculatorkt.ui.theme.TipCalculatorktTheme import kotlin.time.times class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { TipCalculatorktTheme { calc() } } } } @Composable fun calc(){ var totalbill by remember{ mutableStateOf("0") } var tipp by remember { mutableStateOf("0") } var ans by remember { mutableStateOf("0.0") } Column (modifier = Modifier.fillMaxSize().padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center){ TextField(value = totalbill, onValueChange = {totalbill =it}, label = {Text(text = "Enter the total bill amount")}) TextField(value = tipp, onValueChange = {tipp =it}, label = {Text(text = "Enter the tip percentage")}) Spacer(modifier = Modifier.height(16.dp)) Button(onClick = {var bill = totalbill.toDouble() var tip = tipp.toDouble() ans = (bill*(tip/100)).toString() }){ Text(text="Calculate the tip") } if(ans=="0.0"){ Text(text="Tip amount is $ans", fontSize = 30.sp) } else{ Text(text = "Tip amount is $ans",fontSize = 20.sp) } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter