package org.geeksforgeeks.demo import android.os.Bundle import androidx.activity.* import androidx.activity.compose.setContent import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.* import androidx.compose.foundation.text.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.* import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.* import androidx.compose.ui.text.input.* import androidx.compose.ui.text.style.* import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.* import org.geeksforgeeks.demo.ui.theme.DemoTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { DemoTheme { Surface(color = MaterialTheme.colorScheme.background) { MyTextFieldUI() } } } } } @Composable fun MyTextFieldUI() { // we are creating a variable for // getting a value of our text field. val inputValue = remember { mutableStateOf(TextFieldValue()) } Column( // we are using column to align our // textField to center of the screen. modifier = Modifier .fillMaxWidth() .fillMaxHeight(), // below line is used for specifying // vertical arrangement. verticalArrangement = Arrangement.Center, // below line is used for specifying // horizontal arrangement. horizontalAlignment = Alignment.CenterHorizontally, ) { TextField( // below line is used to get // value of text field, value = inputValue.value, // below line is used to get value in text field // on value change in text field. onValueChange = { inputValue.value = it }, // modifier is used to add padding // to our text field. modifier = Modifier .padding(all = 16.dp) .fillMaxWidth(), // below line is used to specify TextField is enabled, allowing user interaction. enabled = true, // below line is used to specify TextField is not read-only, allowing text input. readOnly = false, // below line is used to specify // styling for our text field value. textStyle = TextStyle( color = Color.Black, // below line is used to add font // size for our text field fontSize = 15.sp, fontWeight = FontWeight.SemiBold, fontStyle = FontStyle.Normal, // below line is used to change font family. fontFamily = FontFamily.SansSerif, letterSpacing = 0.5.sp, textDecoration = TextDecoration.None, textAlign = TextAlign.Start ), // A composable displaying a label for the TextField. label = { Text("Label") }, // below line is used to add placeholder // for our text field. placeholder = { Text("Enter user name") }, // leading icon is used to add icon // at the start of text field. leadingIcon = { // In this method we are specifying // our leading icon and its color. Icon(Icons.Filled.AccountCircle, contentDescription = null, tint = Color(0xFF6200EE)) }, // trailing icons is used to add // icon to the end of text field. trailingIcon = { Icon(Icons.Filled.Info, contentDescription = null, tint = Color(0xFF6200EE)) }, // below line is used to indicate the TextField is not in an error state. isError = false, // below line is used to indicate no visual transformation is applied to the text. visualTransformation = VisualTransformation.None, // keyboard options is used to modify // the keyboard for text field. keyboardOptions = KeyboardOptions( // below line is used for capitalization // inside our text field. capitalization = KeyboardCapitalization.None, // below line is used to enable auto // correct in our keyboard. autoCorrect = true, // below line is used to specify our // type of keyboard such as text, number, phone. keyboardType = KeyboardType.Text, imeAction = ImeAction.Done ), keyboardActions = KeyboardActions( onDone = { // Handle the done action } ), // single line boolean is used to avoid // text field entering in multiple lines. singleLine = true, // below line is used to give // max lines for our text field. maxLines = 2, // below line is used to give // max lines for our text field. minLines = 1, // below line uses a MutableInteractionSource for handling interaction states. interactionSource = remember { MutableInteractionSource() }, // below line is used for the shape of the TextField is set to the medium shape from the theme. shape = MaterialTheme.shapes.medium, // below line is used to specify background // color for our text field. colors = TextFieldDefaults.colors( focusedTextColor = Color.Green, disabledTextColor = Color.Green, focusedContainerColor = Color.LightGray, unfocusedContainerColor = Color.LightGray, cursorColor = Color.Blue, errorCursorColor = Color.Red, focusedIndicatorColor = Color.Transparent, unfocusedIndicatorColor = Color.Transparent ) ) Spacer(modifier = Modifier.height(16.dp)) // Displaying the text entered by the user Text(text = "You entered: ${inputValue.value.text}") } } // @Preview function is used to see preview // for our composable function in preview section @Preview(showBackground = true) @Composable fun DefaultPreview() { DemoTheme { MyTextFieldUI() } }