Tue. Dec 3rd, 2024
  • State Managementremember and mutableStateOf are used to hold the state of each field.
  • OutlinedTextField: Used for each text input, with keyboardOptions 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()
}

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 *