Composables and Layouts

Android Development Fundamentals with Compose

Course 4 ยท Chapter 3 ยท Arranging Things on the Screen

A real screen is more than one line of text. In Compose you arrange composables by nesting them inside three layout composables: Row (side by side), Column (one above another) and Box (stacked on top of each other). You then adjust each piece with modifiers. This chapter covers those three, the modifiers you will use most, and a few Material 3 components.

What was checked, and how: read this first
Checked in the docs (one web search, summaries only): the roles of Row, Column and Box; that modifier order matters; fillMaxWidth, size, padding, weight, Arrangement, Alignment and Spacer. Not found in the docs search: Material 3 Card and Scaffold (described from general knowledge). All code is unrun (no Kotlin compiler on the machine used to write this): the imports are listed so you can check them, but expect to fix one or two with Alt+Enter. Nothing here was seen on your install.

Row, Column and Box

ComposableArranges its childrenLike in XML (general knowledge)
ColumnOne below anotherA vertical LinearLayout
RowSide by sideA horizontal LinearLayout
BoxStacked, later children on topA FrameLayout

(docs) By default a layout is only as big as its content. You nest them to build a screen: here a Row holds a coloured circle (a Box) and a Column of two lines of text.

import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.CircleShape import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp
@Composable fun ProfileRow(name: String, role: String) { Row( modifier = Modifier.fillMaxWidth().padding(16.dp), verticalAlignment = Alignment.CenterVertically ) { Box( modifier = Modifier .size(48.dp) .background(MaterialTheme.colorScheme.primary, CircleShape) ) Spacer(modifier = Modifier.width(12.dp)) Column { Text(text = name, style = MaterialTheme.typography.titleMedium) Text(text = role, style = MaterialTheme.typography.bodyMedium) } } }

Modifiers

A Modifier is a chain of instructions you attach to a composable: its size, spacing, background, what happens on a click. You write them one after another with dots. Common ones:

ModifierEffect
padding(16.dp)(docs) Adds space around the composable.
fillMaxWidth()(docs) Takes the full width of the parent.
size(48.dp)(docs) Gives an exact size.
background(color, shape)Paints behind the composable (general knowledge).
weight(1f)(docs) Inside a Row or Column, shares the free space by ratio.
clickable { }Makes any composable respond to taps (general knowledge; Chapter 5).

Order matters

(docs) A modifier chain is applied in sequence, so changing the order changes the result. The classic case is padding and background:

// Padding then background: the coloured area is INSIDE the padding (no coloured margin) Text("A", Modifier.padding(16.dp).background(Color.Yellow)) // Background then padding: the colour fills the padded area too Text("B", Modifier.background(Color.Yellow).padding(16.dp))

In A, padding is applied first, so the yellow covers only the text. In B the yellow is applied first and the padding then grows inside it, so the yellow includes the space. (my reading of the docs' principle; check it in the preview)

Weight

(docs) The official example gives one child twice the weight of another so it receives twice the width:

Row(Modifier.fillMaxWidth()) { Text("One third", Modifier.weight(1f)) Text("Two thirds", Modifier.weight(2f)) }

Arrangement and Alignment

(docs) Arrangement controls how children are distributed along the layout's main axis (horizontal for a Row, vertical for a Column); Alignment controls the cross axis.

Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Text("Left") Text("Right") } Column( modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(8.dp) ) { Text("One") Text("Two") }
LayoutMain axisCross axis
RowhorizontalArrangementverticalAlignment
ColumnverticalArrangementhorizontalAlignment

Arrangement.SpaceBetween, SpaceEvenly and spacedBy(8.dp) are the ones you will use most (general knowledge). Use Spacer(Modifier.width(12.dp)) to put a fixed gap between two items, as in the profile row above (docs: the tutorial does this).

Material 3 Components

(general knowledge, not found by my search) The generated project uses Material 3, whose components (Text, Button, Card, Scaffold) live in androidx.compose.material3. Text and Button you have already met. Card is a raised container with rounded corners; Scaffold (Chapter 1) is the screen skeleton. Text styles come from the theme: MaterialTheme.typography.titleMedium and the like (Chapter 8).

Card(modifier = Modifier.fillMaxWidth().padding(16.dp)) { Column(Modifier.padding(16.dp)) { Text("Card title", style = MaterialTheme.typography.titleMedium) Text("Some text inside a Material 3 card.") } }

Compared with the XML Way

Views and XML (Android Development Fundamentals)Compose
Layout containersLinearLayout, FrameLayout, ConstraintLayoutRow, Column, Box (and ConstraintLayout as a library for complex cases)
Sizes and spacingAttributes such as layout_width, paddingModifiers: fillMaxWidth(), padding()
Reusing a piece of UIAn <include> or a custom viewA function you call
Nesting depthDeep nesting was a performance concernNesting is normal and cheap (general knowledge)
Compared with the Classic UI course
Android Development Fundamentals 3 teaches LinearLayout, ConstraintLayout, common Views and view binding in XML. Here the same jobs are done with Row, Column, Box and modifiers; view binding is not needed, because there are no views to look up. The old Layout Editor's job is done by the preview (Android Studio 2: The New UI 5).

Try It Yourself

Exercise 1

Build a ProfileCard(name, role) that shows a coloured circle on the left and the two lines of text on the right, inside a Material 3 Card. Add a @Preview and check it.

๐Ÿ“„ View solution
Exercise 2

Make a row of three buttons spread evenly across the screen width. Then change it so the first button is pushed to the left and the other two to the right, and say which Arrangement you used for each.

๐Ÿ“„ View solution
Exercise 3

Use a Box to draw a grey square with a small red circular badge showing "3" in its top-right corner. Then swap the order of two modifiers on the badge and describe what changes in the preview.

๐Ÿ“„ View solution
Not Yet Verified
Checked in the docs (one web search, summaries only): the roles of Row, Column and Box; that modifier order matters; fillMaxWidth, size, padding, weight (the 2:1 example), Arrangement and Alignment as main and cross axis; Spacer between items.
Not found in the docs search: Material 3 Card and Scaffold; the background, clickable modifiers and the XML comparison column (general knowledge).
Not verified: the padding-then-background explanation (my reading, check it in the preview); the imports, which may need adjusting; every listing and solution (unrun); the exercises (not run by me). Nothing in this chapter was checked on your install.
Sources: Compose layout basics (developer.android.com), Compose modifiers (developer.android.com), Constraints and modifier order (developer.android.com), Android Compose Tutorial (developer.android.com).
Coming up
Chapter 4 covers state and recomposition: remember, mutableStateOf, state hoisting and why the UI redraws.