Here’s a complete example of a form in Jetpack Compose containing all the requested fields:
Jetpack Compose Code for Form
@Composable
fun FormExample() {
// State variables to hold user input
var plaintext by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var numericPassword by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") }
var phone by remember { mutableStateOf("") }
var postalAddress by remember { mutableStateOf("") }
var multilineText by remember { mutableStateOf("") }
var time by remember { mutableStateOf("") }
var date by remember { mutableStateOf("") }
var number by remember { mutableStateOf("") }
var signedNumber by remember { mutableStateOf("") }
var decimalNumber by remember { mutableStateOf("") }
// Form layout
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
// Plaintext
OutlinedTextField(
value = plaintext,
onValueChange = { plaintext = it },
label = { Text("Plaintext") }
)
// Password
OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
visualTransformation = PasswordVisualTransformation()
)
// Numeric Password
OutlinedTextField(
value = numericPassword,
onValueChange = { numericPassword = it },
label = { Text("Password (Numeric)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.NumberPassword),
visualTransformation = PasswordVisualTransformation()
)
// Email
OutlinedTextField(
value = email,
onValueChange = { email = it },
label = { Text("Email") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
)
// Phone
OutlinedTextField(
value = phone,
onValueChange = { phone = it },
label = { Text("Phone") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone)
)
// Postal Address
OutlinedTextField(
value = postalAddress,
onValueChange = { postalAddress = it },
label = { Text("Postal Address") }
)
// Multiline Text
OutlinedTextField(
value = multilineText,
onValueChange = { multilineText = it },
label = { Text("Multiline Text") },
modifier = Modifier.height(100.dp),
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Default),
maxLines = 5
)
// Time
OutlinedTextField(
value = time,
onValueChange = { time = it },
label = { Text("Time") },
placeholder = { Text("HH:MM") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
// Date
OutlinedTextField(
value = date,
onValueChange = { date = it },
label = { Text("Date") },
placeholder = { Text("YYYY-MM-DD") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
// Number
OutlinedTextField(
value = number,
onValueChange = { number = it },
label = { Text("Number") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
// Signed Number
OutlinedTextField(
value = signedNumber,
onValueChange = { signedNumber = it },
label = { Text("Number (Signed)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
// Number Decimal
OutlinedTextField(
value = decimalNumber,
onValueChange = { decimalNumber = it },
label = { Text("Number (Decimal)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal)
)
// Submit Button
Button(onClick = {
// Handle form submission
}) {
Text("Submit")
}
}
}
Features:
- OutlinedTextField: Used for all input fields.
- Keyboard Options:
KeyboardType.Text
: For plaintext fields.KeyboardType.NumberPassword
: For numeric passwords.KeyboardType.Email
: For email addresses.KeyboardType.Phone
: For phone numbers.KeyboardType.Decimal
: For decimal numbers.
- Visual Transformations: Password fields use
PasswordVisualTransformation
to obscure the text. - Multiline Text: Configured with
maxLines
and increased height for better readability.
Preview the Form
Wrap it in a @Preview
to test:
@Preview(showBackground = true)
@Composable
fun PreviewFormExample() {
FormExample()
}
Let me know if you need additional functionality or improvements! 🚀