Layout Basics in Jetpack Compose
Last Updated :
18 May, 2025
Jetpack Compose is Google’s modern toolkit for building native Android user interfaces using declarative Kotlin code. It makes UI development faster and easier. One of the most important parts of designing any interface is understanding how to lay out your elements. Compose makes layouts flexible and powerful, offering a much simpler way to arrange UI elements compared to traditional XML. In this article, you'll learn the basics of layout in Jetpack Compose, explore the different layout containers available, and see how to create your own custom layout to fit your app's needs.
What is a Layout in Jetpack Compose?
In Jetpack Compose, a layout is a container that decides how child composables (UI elements) are arranged on the screen. It defines their size and position based on certain rules or constraints. Layouts are written in Kotlin code, making them easier to manage and more readable than traditional XML. Jetpack Compose also provides several built-in layout containers like - Row, Column, Box, and Lazy Layouts that are easy to use and cover most common layout needs.
Key Concepts of Jetpack Compose Layouts:
- Composable Function: In Compose, you can think of every element in the UI as a composable function, and it’s a function that composes UI content.
- Modifier: It is one of the fundamental ideas for establishing how a layout will act and look, including its size, margin, alignment, and background color.
Basic Layout Containers
1. Column
The Column composable arranges its child composables vertically, one below the other. It respects the constraints of its parent and will take up as much vertical space as it needs for its children.
Kotlin
@Preview(showBackground = true)
@Composable
fun ColumnExample() {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
Text(text = "Item 1")
Text(text = "Item 2")
Text(text = "Item 3")
}
}
Preview:
2. Row
Similar to Column, the Row composable arranges its children horizontally in a line, from left to right. It takes up as much horizontal space as necessary to fit its children.
Kotlin
@Preview(showBackground = true)
@Composable
fun RowExample() {
Row(
modifier = Modifier
.fillMaxSize()
.padding(12.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(text = "Left")
Text(text = "Center")
Text(text = "Right")
}
}
Preview:
3. Box
The Box composable allows children to be layered on top of each other, enabling overlays or stacking of elements. By default, each child will be positioned in the top-left corner unless specified otherwise.
Kotlin
@Preview(showBackground = true)
@Composable
fun BoxExample() {
Box(
modifier = Modifier
.background(Color.Gray)
.size(200.dp)
) {
Text(
text = "Top Text",
modifier = Modifier.align(Alignment.TopStart)
)
Text(
text = "Bottom Text",
modifier = Modifier.align(Alignment.BottomEnd)
)
}
}
Preview:
Modifiers and Customization
Modifiers are fundamental in Compose as they allow you to define how your composables behave and appear. You can chain multiple modifiers to achieve a range of effects such as layout control, interaction handling, and appearance styling.
1. Basic Modifiers
- padding(): Adds space around the composable.
- size(): Sets the size of the composable.
- fillMaxSize(): Expands the composable to fill the maximum available space in both dimensions.
- background(): Sets a background color or drawable for the composable.
- clickable(): Makes the composable clickable.
Kotlin
@Preview(showBackground = true)
@Composable
fun ModifierExample() {
Text(
text = "Hello, Jetpack Compose!",
modifier = Modifier
.padding(16.dp)
.background(Color.Green)
.size(150.dp)
)
}
Preview:
2. Alignment and Arrangement
Compose provides flexibility in aligning children within layout containers like Row, Column, and Box.
- Horizontal/Vertical Arrangement: Used in Row or Column to define the spacing between children.
- Alignment: Used in Box or individually on children within Row and Column to control how items align within their container.
Kotlin
@Preview(showBackground = true)
@Composable
fun AlignmentExample() {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
Text(text = "Centered Text")
}
}
Preview:
Custom Layouts
Basically, predefined layouts should be enough in most cases, but sometimes you have to place elements with specific positioning rules into your layout. With the help of Layout composable, Compose let you design your own layouts.
Example of a Custom Layout:
Let’s create a simple custom layout that places one composable in the center and another one below it.
Kotlin
@Composable
fun CustomLayoutExample(
modifier: Modifier = Modifier,
content: @Composable () -> Unit
) {
Layout(
content = content,
modifier = modifier
) { measurables, constraints ->
// Measure children
val placeables = measurables.map { measurable ->
measurable.measure(constraints)
}
// Set the layout size
layout(constraints.maxWidth, constraints.maxHeight) {
// Positioning child composables
placeables.forEachIndexed { index, placeable ->
if (index == 0) {
// Place first child at top-left
placeable.placeRelative(0, 0)
} else {
// Place the second child below the first
placeable.placeRelative(0, placeables[0].height)
}
}
}
}
}
To use the custom layout:
Kotlin
@Preview(showBackground = true)
@Composable
fun Preview() {
CustomLayoutExample (
modifier = Modifier
.padding(16.dp)
) {
Text(text = "Top")
Text(text = "Bottom")
}
}
Preview:
Lazy Layouts
For dynamic and large data sets, LazyColumn and LazyRow are efficient alternatives to Column and Row. These layouts render only the visible items, significantly improving performance for scrollable lists.
LazyColumn
Kotlin
@Preview(showBackground = true)
@Composable
fun LazyColumnExample() {
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
items(100) { index ->
Text(text = "Item #$index", modifier = Modifier.padding(16.dp))
}
}
}
Preview:
LayRow
Kotlin
@Preview(showBackground = true)
@Composable
fun LazyRowExample() {
LazyRow(
modifier = Modifier.fillMaxSize()
) {
items(100) { index ->
Text(text = "Item #$index", modifier = Modifier.padding(16.dp))
}
}
}
Preview:
Conclusion
Better than using XML, the layout in Jetpack Compose is easier to create and adjust for UI layouts in android applications. Apart from basic UI containers such as Column, Row, Box and LazyColumn, it offers powerful modifiers with which you can create responsive and dynamic UI. Custom layouts are useful when needed to give an option for creating extremely targeted and unique designs for your app.With this foundation you will be able to start introducing Compose layouts to build smooth and beautiful user interfaces. It’s declarative which gives you the opportunity to write the UI code in a composing clear and consistently maintainable.
Similar Reads
Basics of Jetpack Compose in Android Jetpack Compose is a modern UI toolkit that is designed to simplify UI development in Android. It consists of a reactive programming model with conciseness and ease of Kotlin programming language. It is fully declarative so that you can describe your UI by calling some series of functions that will
5 min read
Tab Layout in Android using Jetpack Compose Tab Layout is seen used in most applications such as WhatsApp in which users can navigate to multiple screens easily by simply swiping to the left or right. This tab layout provides easy navigation between multiple screens. In this article, we will take a look at How we can implement Tab Layout with
7 min read
Spacer in Android Jetpack Compose In Jetpack Compose, a Spacer is a blank element that is used to create a Space between two UI elements. Suppose, we have created Element 1 and we want to place Element 2 below Element 1 but with a top margin, we can declare a Spacer between the two elements. So in this article, we will show you how
2 min read
Motion Layout Button in Android Jetpack Compose Motion Layout is a special version of Constraint layout. With the help of motion layout, we can add animations to the widgets within the layout and change the position of that widget dynamically. In this article, we will take a look at How to implement Motion Layout animation on buttons in Android u
8 min read
Card in Android Jetpack Compose In Jetpack Compose, a Card serves as a modern alternative to CardView in traditional Android development. It acts as a container that can hold multiple UI elements, making it ideal for structuring content in a visually appealing way. The Card component includes an elevation property, which adds a sh
2 min read
Scaffold in Android using Jetpack Compose Scaffold in Android Jetpack is a composable function that provides a basic structure of the layout. It holds together different parts of the UI elements such as Application bars, floating action buttons, etc.There are a lot of apps that contain TopAppBar, Drawer, Floating Action Button, BottomAppBar
3 min read