Jetpack Compose: UI Components
Jetpack Compose is a modern, fully declarative UI toolkit for Android that simplifies the UI development process. It allows developers to build native UIs with Kotlin code, making it easier to create and maintain apps. In this blog, we’ll explore some common UI widgets in Jetpack Compose, such as Text, Image, Button, RadioButton, Checkbox, and TextField, and demonstrate how to use them in Compose with examples. If you're transitioning from XML-based layouts, we'll also briefly compare these components to their XML counterparts.
1. TextView (Jetpack Compose’s Text)
In Jetpack Compose, the Text composable is used to display text on the screen. It is more flexible than the XML TextView, as you can easily style and manipulate the text directly with Kotlin code.
Example:
Text(
text = "Hello, Jetpack Compose!",
style = MaterialTheme.typography.h6,
color = Color.Black
)In this example:
textis the string to display.styleis used to apply predefined text styles likeh6, which defines the text size and weight.colorallows customization of the text color.
Comparison with XML:
- In XML, you would define a
TextViewand set properties likeandroid:text,android:textSize, andandroid:textColor.
2. ImageView (Jetpack Compose’s Image)
The Image composable is used for displaying images in Jetpack Compose. It can load images from various sources, including resources, URIs, and assets.
Example:
Image(
painter = painterResource(id = R.drawable.my_image),
contentDescription = "An image from resources",
modifier = Modifier.size(200.dp)
)painteris used to load the image.contentDescriptionis important for accessibility, especially for users with visual impairments.modifiercontrols the size, alignment, and other layout properties of the image.
Comparison with XML:
- In XML,
ImageViewis used, and you set the image usingandroid:srcorandroid:background.
3. Button (Jetpack Compose’s Button)
A Button in Jetpack Compose is simple to use and allows you to define actions when clicked. It's declarative and easy to customize.
Example:
Button(onClick = { /* Handle click action */ }) {
Text("Click Me")
}- The
onClicklambda defines the action to be performed when the button is clicked. - The content inside the button is defined by the
Textcomposable.
Comparison with XML:
- In XML, you would use a
Buttonand define anandroid:onClickattribute for actions.
Get Manish Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
4. RadioButton (Jetpack Compose’s RadioButton)
A RadioButton allows users to select one option from a set of choices. It's similar to the RadioButton in XML, but the state management is easier in Compose.
Example:
var selectedOption by remember { mutableStateOf("option1") }
Column {
RadioButton(
selected = selectedOption == "option1",
onClick = { selectedOption = "option1" }
)
Text("Option 1")
RadioButton(
selected = selectedOption == "option2",
onClick = { selectedOption = "option2" }
)
Text("Option 2")
}selecteddetermines whether the radio button is selected.onClickchanges the selected option.- State management in Compose is simpler, using
rememberandmutableStateOf.
Comparison with XML:
- In XML, you would use
RadioGroupandRadioButton, managing state and selection manually with listeners. - Compose automatically handles the state for you, making it more concise and easier to manage.
5. Checkbox (Jetpack Compose’s Checkbox)
The Checkbox composable in Jetpack Compose allows users to select or deselect an option. It is straightforward to use and offers declarative state management.
Example:
var isChecked by remember { mutableStateOf(false) }
Checkbox(
checked = isChecked,
onCheckedChange = { isChecked = it }
)checkedrepresents the state of the checkbox.onCheckedChangeis called when the checkbox is clicked, allowing you to toggle its state.
Comparison with XML:
- In XML, you’d use a
CheckBoxand handle state changes with asetOnCheckedChangeListener. - Compose simplifies this with a more declarative approach, allowing state management to be handled with
mutableStateOf.
6. EditText (Jetpack Compose’s TextField)
The TextField composable is used for user input, similar to the EditText in XML. It supports both single-line and multi-line input, and it can be customized with various styles and actions.
Example:
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
label = { Text("Enter text") },
modifier = Modifier.fillMaxWidth()
)valueholds the current text in the field.onValueChangeupdates the text state when the user types.labelis used to show a label inside the text field.
Comparison with XML:
- In XML, you’d use
EditTextand define properties likeandroid:text,android:hint, andandroid:inputType. - Compose’s
TextFieldprovides better state management and flexibility with the UI, making it easier to handle input dynamically.
To See the Live rendering in your Android studio. You can checkout the codebase from my repository composeDemo. Open
UiWidgetDemoclass and see the preview.
Happy coding!!





