Thu. Nov 7th, 2024
  • LocalContext.current: This provides the context needed to show a Toast.
  • Toast in onClick: The Toast.makeText() function is called when the Submit button is clicked, displaying all entered data.
  • Message Formatting: Each field value is concatenated into a single message with line breaks for readability.

Now, when you click the Submit button, the entered information will appear in a Toast message.

package com.androindian.jetpack

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
UserForm()
}
}
}

@Composable
fun UserForm() {
var name by remember { mutableStateOf("") }
var mobile by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }

// Get the context to show a Toast
val context = LocalContext.current

Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
// Name Field
OutlinedTextField(
value = name,
onValueChange = { name = it },
label = { Text("Name") },
modifier = Modifier.fillMaxWidth()
)

// Mobile Field
OutlinedTextField(
value = mobile,
onValueChange = { mobile = it },
label = { Text("Mobile") },
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone)
)

// Email Field
OutlinedTextField(
value = email,
onValueChange = { email = it },
label = { Text("Email") },
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
)

// Password Field
OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
modifier = Modifier.fillMaxWidth(),
visualTransformation = PasswordVisualTransformation()
)

// Submit Button
Button(
onClick = {
// Display the entered data in a Toast
Toast.makeText(
context,
"Name: $name\nMobile: $mobile\nEmail: $email\nPassword: $password",
Toast.LENGTH_LONG
).show()
},
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp)
) {
Text("Submit")
}
}
}

@Preview(showBackground = true)
@Composable
fun PreviewUserForm() {
UserForm()
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *