BillTip package com.example.billtip 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.fillMaxSize 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 com.example.billtip.ui.theme.BillTipTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { Calculate() } } } //100 1% 1rs @Composable fun Calculate(){ var totalBill by remember { mutableStateOf("0") } var tipPercentage 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 Total Bill Amount")} ) TextField(value = tipPercentage, onValueChange = {tipPercentage = it}, label = { Text(text = "Enter Tip Percentage")} ) Button(onClick = { var bill = totalBill.toDouble() var tip = tipPercentage.toDouble() ans = bill*(tip/100) }) { Text(text = "Calculate Tip Amount") } Text(text = "Tip Amount is ${ans}") } }
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