Introduction
This Jetpack Compose code demonstrates how to create a text field that allows users to enter text and displays the entered text below the field. It also showcases how to handle changes in the text field using state variables.
Breakdown
Import necessary libraries:
androidx.appcompat.app.AppCompatActivity: This library provides the base class for activities in Android.androidx.compose.*: This library imports all the Compose UI components and utilities.
Create a new composable function 'MainContent':
- This function creates a
Columnlayout with padding of 25dp. - It uses the
remembercomposable to create a mutable state variabletextStateof typeTextFieldValue, which is initially empty. This state variable will store the current text entered by the user in the text field.
- This function creates a
Text Field:
- A
TextFieldcomposable is used to display a label "Enter Country" and a text field for user input.- The
valueparameter of theTextFieldis set to the current value of thetextStatevariable. This means that the text displayed in the text field will reflect the current state of thetextStatevariable. - The
onValueChangeparameter is a callback function that gets called whenever the user changes the text in the text field. When the user types new text, this callback function is triggered, and the new text is stored in thetextStatevariable. - The
labelparameter is set to aTextcomposable that displays the label "Enter Country". This label helps users understand what kind of information they should enter in the text field. - The
keyboardOptionsparameter is set to specify that the keyboard should be in text mode, which is appropriate for entering country names. - The
textStyleparameter is used to style the text in the text field. In this case, it's set to Monospace font, bold weight, 25sp font size, and normal style.
- The
- A
Display Entered Text:
- Below the
TextField, aTextcomposable is used to display the entered text. The text displayed is "You entered: " followed by the current value of thetextState.text. ThisTextcomposable shows the user what they have entered in the text field. - The text style for this
Textcomposable is also set using thetextStyleparameter.
- Below the
Set Content:
- Finally, the
setContentfunction is called to set the content of the activity. It takes theMainContentcomposable as its argument. This sets the UI of the activity to the content defined in theMainContentcomposable.
- Finally, the
Summary
This code effectively demonstrates how to create a user-friendly text field experience in a Jetpack Compose application. By leveraging state variables and callbacks, the code ensures that the displayed text remains up-to-date with the user's input. The code also incorporates visual styling to enhance the appearance of the text field and the entered text.
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.foundation.text.KeyboardOptions
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.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
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(text = "Enter Country") },
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text
),
textStyle = TextStyle(
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Bold,
fontSize = 25.sp,
fontStyle = FontStyle.Normal
)
)
Text(
text = "You entered : ${textState.value.text}",
fontSize = 22.sp,
color = Color.Blue,
fontFamily = FontFamily.SansSerif,
fontStyle = FontStyle.Italic,
modifier = Modifier.padding(top = 25.dp)
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - How to use Text
- jetpack compose - ClickableText example
- jetpack compose - How to use Button
- jetpack compose - How to use TextField
- jetpack compose - OutlinedTextField example
- jetpack compose - Password TextField example
- jetpack compose - How to use Card
- jetpack compose - How to use Checkbox
- jetpack compose - How to use RadioButton
- jetpack compose - Image scale
- jetpack compose - Image background
- jetpack compose - Image from URL
- jetpack compose - LazyColumn scroll to position
- jetpack compose - LazyColumn alternate item color
- jetpack compose - LazyColumn sticky header