Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Jetpack Compose is Android's modern UI toolkit for building native UI. It simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.
To use an EditText
equivalent in Jetpack Compose, you'd be using the TextField
composable. Here's how you can do it:
Ensure you have Jetpack Compose set up in your Android project. Your build.gradle
should have the necessary dependencies and Kotlin compiler plugin for Compose.
Here's a simple example of how to use TextField
in Jetpack Compose:
import androidx.compose.runtime.* import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.window.setContent import androidx.activity.ComponentActivity import androidx.compose.material.* import androidx.compose.ui.graphics.Color @Composable fun EditTextDemo() { var text by remember { mutableStateOf("Hello Compose") } TextField( value = text, onValueChange = { text = it }, label = { Text("Enter something") }, colors = TextFieldDefaults.textFieldColors( backgroundColor = Color.White ) ) } @Preview fun PreviewEditTextDemo() { EditTextDemo() }
In this code:
text
which will hold the text value of our TextField
.TextField
composable, which is equivalent to EditText
in the traditional Android view system.value
property represents the current text, and onValueChange
is a callback that updates our state-backed property whenever the text changes.label
to provide a hint (equivalent to hint
in EditText
).colors
parameter can be used to customize the appearance, in this case setting a white background.To use this composable in an Activity:
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MaterialTheme { EditTextDemo() } } } }
Remember to wrap your composable UI in MaterialTheme
to provide theming to the components and benefit from Material design styling.
With Jetpack Compose, building UI becomes more straightforward and idiomatic to Kotlin, reducing boilerplate and increasing readability.
Creating EditText with Jetpack Compose:
EditText
in Jetpack Compose, you can use the TextField
composable.import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.TextFieldDefaults import androidx.compose.material3.icons.Icons import androidx.compose.material3.icons.filled.MailOutline import androidx.compose.material3.icons.filled.Person import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.tooling.preview.Preview @Composable fun EditTextExample() { OutlinedTextField( value = "Hello, World!", onValueChange = { /* Handle value change */ }, label = { /* Label for the EditText */ }, keyboardOptions = KeyboardOptions.Default.copy( imeAction = ImeAction.Done ), keyboardActions = KeyboardActions(onDone = { /* Handle Done action */ }), leadingIcon = { Icons.Default.Person }, trailingIcon = { Icons.Default.MailOutline }, modifier = Modifier.fillMaxWidth() ) } @Preview @Composable fun PreviewEditTextExample() { EditTextExample() }
Handling User Input in Jetpack Compose EditText:
onValueChange
callback to handle user input changes in the EditText
.@Composable fun EditTextWithInputHandling() { var text by remember { mutableStateOf("Initial Text") } OutlinedTextField( value = text, onValueChange = { text = it // Handle user input changes }, label = { /* Label for the EditText */ }, modifier = Modifier.fillMaxWidth() ) }
Customizing EditText Appearance in Jetpack Compose:
EditText
in Jetpack Compose using various modifiers.@Composable fun CustomizedEditText() { var text by remember { mutableStateOf("Initial Text") } OutlinedTextField( value = text, onValueChange = { text = it // Handle user input changes }, label = { /* Label for the EditText */ }, modifier = Modifier .fillMaxWidth() .padding(16.dp) .background(Color.Gray) .border(1.dp, Color.Black) ) }
Listening to Text Changes in Jetpack Compose EditText:
onValueChange
to listen to text changes in Jetpack Compose EditText
.@Composable fun EditTextWithTextChangeListener() { var text by remember { mutableStateOf("Initial Text") } OutlinedTextField( value = text, onValueChange = { text = it // Handle text changes }, label = { /* Label for the EditText */ }, modifier = Modifier.fillMaxWidth() ) }
Setting Up Keyboard Input Type in Jetpack Compose:
keyboardOptions
in Jetpack Compose.@Composable fun EditTextWithKeyboardType() { var text by remember { mutableStateOf("Initial Text") } OutlinedTextField( value = text, onValueChange = { text = it // Handle user input changes }, label = { /* Label for the EditText */ }, keyboardOptions = KeyboardOptions.Default.copy( keyboardType = KeyboardType.Password ), modifier = Modifier.fillMaxWidth() ) }
Using State for EditText Values in Jetpack Compose:
remember
and mutableStateOf
to manage state for EditText
values in Jetpack Compose.@Composable fun EditTextWithState() { var text by remember { mutableStateOf("Initial Text") } OutlinedTextField( value = text, onValueChange = { text = it // Handle user input changes }, label = { /* Label for the EditText */ }, modifier = Modifier.fillMaxWidth() ) }
Styling and Theming EditText in Jetpack Compose:
EditText
in Jetpack Compose using the Modifier
and custom styles.@Composable fun StyledEditText() { var text by remember { mutableStateOf("Initial Text") } OutlinedTextField( value = text, onValueChange = { text = it // Handle user input changes }, label = { /* Label for the EditText */ }, modifier = Modifier .fillMaxWidth() .padding(16.dp) .background(MaterialTheme.colorScheme.primary) .border(1.dp, MaterialTheme.colorScheme.secondary) ) }
Working with Focus and Keyboard in Jetpack Compose EditText:
Modifier
and focusRequester
.@Composable fun EditTextWithFocusAndKeyboard() { var text by remember { mutableStateOf("Initial Text") } val focusRequester = remember { FocusRequester() } OutlinedTextField( value = text, onValueChange = { text = it // Handle user input changes }, label = { /* Label for the EditText */ }, modifier = Modifier .fillMaxWidth() .padding(16.dp) .focusRequester(focusRequester) ) // Set focus programmatically Button(onClick = { focusRequester.requestFocus() }) { Text("Set Focus") } }