// GroceryItemsMap Example // Source code file: MainActivity.kt package it372.ssmith.groceryitemsmap import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.text.TextStyle import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MainLayout( ) } } } @Composable fun MainLayout( ) { val items = mapOf( 34567 to GroceryItem("Cabbage", 34567, 2.59), 46576 to GroceryItem("Tomatos", 46576, 3.55), 76543 to GroceryItem("Onions", 76543, 2.58) ) var tfKey = remember { mutableStateOf("0") } var textContents = remember { mutableStateOf("") } Column(modifier = Modifier.padding(all = 40.dp)) { // Textfield contains a length in inches. val valueInch = null TextField(value = tfKey.value, label = { Text("Enter key for groceries map:") }, onValueChange = { tfKey.value = it }, modifier = Modifier .padding(bottom = 50.dp, top = 50.dp, start = 20.dp, end = 20.dp) .size(width = 200.dp, height = 75.dp) .fillMaxWidth( ), textStyle = TextStyle.Default.copy(fontSize = 28.sp) ) Button( onClick = { var key = 0 tfKey.value.toIntOrNull( )?.let { key = it } textContents.value = if (key in items) items[key].toString( ) else "" }, modifier = Modifier.padding(all = 50.dp)) { Text("Show Grocery Item") } Text(text = textContents.value) } } @Preview(showBackground = true) @Composable fun MainLayoutPreview() { MainLayout( ) }