- State Management:
remember
andmutableStateOf
are used to hold the state of each field. OutlinedTextField
: Used for each text input, withkeyboardOptions
set for Mobile (phone type) and Email (email type) fields.PasswordVisualTransformation
: Hides the password input.- Button: Triggers the submission action.
The Column
arranges all elements vertically, with padding and spacing between fields.
package com.androindian.jetpack
import android.os.Bundle
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.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("") }
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 = { /* Handle submit action */ },
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp)
) {
Text("Submit")
}
}
}
@Preview(showBackground = true)
@Composable
fun PreviewUserForm() {
UserForm()
}