fcamuso

App Android video 06

Sep 18th, 2025
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.72 KB | None | 0 0
  1. package com.example.convertitoreculinario
  2.  
  3. import android.os.Bundle
  4. import androidx.activity.ComponentActivity
  5. import androidx.activity.compose.setContent
  6. import androidx.activity.enableEdgeToEdge
  7. import androidx.compose.foundation.layout.Arrangement
  8. import androidx.compose.foundation.layout.Box
  9. import androidx.compose.foundation.layout.Column
  10. import androidx.compose.foundation.layout.Row
  11. import androidx.compose.foundation.layout.Spacer
  12. import androidx.compose.foundation.layout.fillMaxSize
  13. import androidx.compose.foundation.layout.height
  14. import androidx.compose.foundation.layout.width
  15. import androidx.compose.material.icons.Icons
  16. import androidx.compose.material.icons.filled.ArrowDropDown
  17. import androidx.compose.material3.Button
  18. import androidx.compose.material3.DropdownMenu
  19. import androidx.compose.material3.DropdownMenuItem
  20. import androidx.compose.material3.Icon
  21. import androidx.compose.material3.MaterialTheme
  22. import androidx.compose.material3.OutlinedTextField
  23. import androidx.compose.material3.Surface
  24. import androidx.compose.material3.Text
  25. import androidx.compose.runtime.Composable
  26. import androidx.compose.runtime.mutableStateOf
  27. import androidx.compose.runtime.remember
  28. import androidx.compose.ui.Alignment
  29. import androidx.compose.ui.Modifier
  30. import androidx.compose.ui.tooling.preview.Preview
  31. import androidx.compose.ui.unit.dp
  32. import com.example.convertitoreculinario.ui.theme.ConvertitoreCulinarioTheme
  33.  
  34. class MainActivity : ComponentActivity() {
  35.     override fun onCreate(savedInstanceState: Bundle?) {
  36.         super.onCreate(savedInstanceState)
  37.         enableEdgeToEdge()
  38.         setContent {
  39.             ConvertitoreCulinarioTheme {
  40.                 Surface(modifier = Modifier.fillMaxSize()) {
  41.                     ConvertitoreCulinario()
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. @Composable
  49. fun ConvertitoreCulinario() {
  50.     var daSrotolata = remember { mutableStateOf(false) }
  51.     var aSrotolata = remember { mutableStateOf(false) }
  52.  
  53.     var inputCasella = remember { mutableStateOf(value="") }
  54.  
  55.     var daUnita = remember { mutableStateOf(value="") }
  56.     var aUnita = remember { mutableStateOf(value="") }
  57.  
  58.     var conversione = remember { mutableStateOf(value="") }
  59.  
  60.     fun Converti() : String {
  61.         val input = inputCasella.value.toDoubleOrNull() ?: 0.0
  62.  
  63.         val risultatoConversione = when (daUnita.value) {
  64.             "Tazze" -> when (aUnita.value) {
  65.                 "ml" -> input * 200
  66.                 "grammi" -> input * 125
  67.                 else -> ""
  68.             }
  69.  
  70.             "Bicchieri" -> when (aUnita.value) {
  71.                 "ml" -> input * 200
  72.                 "grammi" -> input * 125
  73.                 else -> ""
  74.             }
  75.  
  76.             "Cucchiai" -> when (aUnita.value) {
  77.                 "ml" -> input * 200
  78.                 "grammi" -> input * 125
  79.                 else -> ""
  80.             }
  81.  
  82.             else -> ""
  83.         }
  84.  
  85.         return risultatoConversione.toString()
  86.     }
  87.  
  88.     Column(
  89.         modifier = Modifier.fillMaxSize(),
  90.         verticalArrangement = Arrangement.Center,
  91.         horizontalAlignment = Alignment.CenterHorizontally
  92.     ) {
  93.  
  94.         Text("Convertitore Culinario", style = MaterialTheme.typography.headlineLarge)
  95.         Spacer(modifier = Modifier.height(16.dp))
  96.  
  97.         OutlinedTextField(
  98.             value = inputCasella.value,
  99.             onValueChange = {
  100.                 inputCasella.value = it
  101.                 conversione.value = Converti()
  102.             },
  103.             label = {Text("Enter Value") })
  104.  
  105.         Spacer(modifier = Modifier.height(16.dp))
  106.  
  107.         Row {
  108.             Box {
  109.  
  110.                 Button(onClick = { daSrotolata.value = true }) {
  111.                     Text(text = daUnita.value)
  112.                     Icon(
  113.                         Icons.Default.ArrowDropDown,
  114.                         contentDescription = "Freggia giú"
  115.                     )  //accessibilitá ...
  116.                 }
  117.  
  118.                 DropdownMenu(expanded = daSrotolata.value, onDismissRequest = {
  119.                              daSrotolata.value = false})
  120.                 {
  121.                     DropdownMenuItem(
  122.                         text = { Text("Tazze") },
  123.                         onClick = {
  124.                             daSrotolata.value = false
  125.                             daUnita.value = "Tazze"
  126.                             conversione.value = Converti()
  127.                         }
  128.                     )
  129.  
  130.                     DropdownMenuItem(
  131.                         text = { Text("Cucchiai") },
  132.                         onClick = {
  133.                             daSrotolata.value = false
  134.                             daUnita.value = "Cucchiai"
  135.                             conversione.value = Converti()
  136.                         }
  137.                     )
  138.  
  139.                     DropdownMenuItem(
  140.                         text = { Text("Bicchieri") },
  141.                         onClick = {
  142.                             daSrotolata.value = false
  143.                             daUnita.value = "Bicchieri"
  144.                             conversione.value = Converti()
  145.                         }
  146.                     )
  147.                 }
  148.             }
  149.  
  150.             Spacer(modifier = Modifier.width(16.dp))
  151.  
  152.             Box {
  153.                 Button(onClick = {aSrotolata.value = true }) {
  154.                     Text(text = aUnita.value)
  155.                     Icon(
  156.                         Icons.Default.ArrowDropDown,
  157.                         contentDescription = "Freggia giú"
  158.                     )  //accessibilitá ...
  159.                 }
  160.  
  161.                 DropdownMenu(expanded = aSrotolata.value, onDismissRequest = {
  162.                     aSrotolata.value = false
  163.                 }) {
  164.  
  165.                     DropdownMenuItem(
  166.                         text = { Text("ml") },
  167.                         onClick = {
  168.                             aSrotolata.value = false
  169.                             aUnita.value = "ml"
  170.                             conversione.value = Converti()
  171.                         }
  172.                     )
  173.  
  174.                     DropdownMenuItem(
  175.                         text = { Text("grammi") },
  176.                         onClick = {
  177.                             aSrotolata.value = false
  178.                             aUnita.value = "grammi"
  179.                             conversione.value = Converti()
  180.                         }
  181.                     )
  182.                 }
  183.  
  184.             }
  185.  
  186.         }
  187.         Spacer(modifier = Modifier.height(30.dp))
  188.         Text("Pari a:" + conversione.value,
  189.             style = MaterialTheme.typography.headlineMedium
  190.         )
  191.     }
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. @Preview(showBackground = true)
  200. @Composable
  201. fun ConvertitoreCulinarioPreview() {
  202.     ConvertitoreCulinarioTheme {
  203.         ConvertitoreCulinario()
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment