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.
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
| Composable | Arranges its children | Like in XML (general knowledge) |
|---|---|---|
Column | One below another | A vertical LinearLayout |
Row | Side by side | A horizontal LinearLayout |
Box | Stacked, later children on top | A 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.
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:
| Modifier | Effect |
|---|---|
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:
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:
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.
| Layout | Main axis | Cross axis |
|---|---|---|
Row | horizontalArrangement | verticalAlignment |
Column | verticalArrangement | horizontalAlignment |
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).
Compared with the XML Way
| Views and XML (Android Development Fundamentals) | Compose | |
|---|---|---|
| Layout containers | LinearLayout, FrameLayout, ConstraintLayout | Row, Column, Box (and ConstraintLayout as a library for complex cases) |
| Sizes and spacing | Attributes such as layout_width, padding | Modifiers: fillMaxWidth(), padding() |
| Reusing a piece of UI | An <include> or a custom view | A function you call |
| Nesting depth | Deep nesting was a performance concern | Nesting is normal and cheap (general knowledge) |
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
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.
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 solutionUse 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.
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).
remember, mutableStateOf, state hoisting and why the UI redraws.