Jetpack Compose - Understanding the TextField Example
This code demonstrates the usage of TextField
composable in Jetpack Compose. It creates a simple text input field where users can enter their name and displays the entered text below the field. Let's break down the code step-by-step:
Setting Up the Activity:
- The code defines a basic
MainActivity
class that extendsAppCompatActivity
. - In the
onCreate
method, it usessetContent
to set the composable content for the activity. This content is defined in theMainContent
composable function.
- The code defines a basic
Main Content Composable:
- The
MainContent
function is a composable that defines the UI structure of the screen. - It uses a
Column
layout to arrange elements vertically with padding around it.
- The
Managing User Input:
- Inside the
Column
, a state variable namedtextState
is created usingremember
composable. This variable holds the current text entered by the user as aTextFieldValue
object.
- Inside the
Creating the Text Field:
- A
TextField
composable is used to create the input field. - It takes three main arguments:
value
: This is set to the current state oftextState
.onValueChange
: This defines a callback function that gets called whenever the user changes the text in the field. The function updates thetextState
with the new entered text.label
: This sets the label displayed above the text field, which is "Input Name" in this example.
- A
Displaying Input:
- A
Text
composable is used below theTextField
to display the entered text. It retrieves the text fromtextState.value.text
and displays it with a red color and monospace font.
- A
Preview:
- The
ComposablePreview
function allows for a preview of the composable UI in Android Studio's layout preview window. While commented out in this example, it can be used to see the UI without running the app.
- The
Summary
This example showcases the basic functionalities of TextField
in Jetpack Compose. It demonstrates how to capture user input, manage the state of the input, and display the entered text on the screen. You can further customize the TextField
with additional features like password masking, keyboard options, and visual transformations for specific use cases.
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@Composable
fun MainContent(){
Column(Modifier.padding(25.dp)) {
val textState = remember{ mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textState.value = it },
label = { Text("Input Name") }
)
Text(
text = "You inputted : " + textState.value.text,
fontSize = 20.sp,
color = Color.Red,
fontFamily = FontFamily.Monospace
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - How to use Text
- jetpack compose - ClickableText example
- jetpack compose - How to use Button
- jetpack compose - OutlinedTextField example
- jetpack compose - Password TextField example
- jetpack compose - Handle changes in a TextField
- jetpack compose - How to use Card
- jetpack compose - How to use Checkbox
- jetpack compose - How to use RadioButton
- jetpack compose - Column background color
- jetpack compose - Column border
- jetpack compose - Column spacing
- jetpack compose - Column scrollable
- jetpack compose - Row spacing
- jetpack compose - Row scrolling