Mehdi TmimiMobile DevelopmentMobile

Mobile Development Lab 2 Part 2 · parts 0–7

Place the pieces: Column, Row, Box

In Lab 1 you dragged sixteen views and tied their constraints. Here, three containers do it.

The idea: three containers, and a Modifier

Lab 1 placed each view with constraints. In Compose you nest containers: each one holds the next pieces between its { } (Kotlin sheet, card 8).

Lab 1: a vertical LinearLayout

Column

Stacks its pieces, top to bottom.

Lab 1: a horizontal LinearLayout

Row

Lines them up, left to right.

Lab 1: a FrameLayout

Box

Lays them one over the other: the last one on top.

Three containers place the whole screen. Each piece gets its size and its space from its Modifier.

The screen you build, container by container

kotlin+java › com.yourname.notes › NotesScreen.ktRead only
package com.yourname.notes
import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
@Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
// The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } }
// The list's title, and its hint on the right Row(modifier = Modifier.padding(start = 32.dp, top = 24.dp, end = 32.dp)) { Text(text = "YOUR NOTES", modifier = Modifier.alignByBaseline()) Text( text = "Tap to open · hold to delete", textAlign = TextAlign.End, modifier = Modifier .padding(start = 16.dp) .weight(1f) .alignByBaseline() ) } }}
@Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}
  1. The screen: a Column. The Box, then the title row, one under the other.
  2. A Box: the band and the card, one over the other.
  3. The band: a Column, the logo over the title row.
  4. A Row: Notes and its count, on one baseline.
  5. The card: a Column, drawn over the band because it comes last in the Box.
  6. A Row: the counter takes the free space, Clear and Add keep their width.
  7. A Row: YOUR NOTES, and the hint at the right.
The same numbers as the code: 1 the screen, 2 the Box, 3 the band, 4 the title row, 5 the card, 6 its last row, 7 the list’s title.

Read the Modifier top to bottom

kotlin+java › com.yourname.notes › NotesScreen.ktRead only
package com.yourname.notes
import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
@Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
// The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } }
// The list's title, and its hint on the right Row(modifier = Modifier.padding(start = 32.dp, top = 24.dp, end = 32.dp)) { Text(text = "YOUR NOTES", modifier = Modifier.alignByBaseline()) Text( text = "Tap to open · hold to delete", textAlign = TextAlign.End, modifier = Modifier .padding(start = 16.dp) .weight(1f) .alignByBaseline() ) } }}
@Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}
  1. padding first: 16 dp left empty around the band. A margin.
  2. 168 dp high, across the whole width.
  3. background paints what is left.
  4. padding after the paint: the logo starts 16 dp from the left and 20 from the top, inside the orange.
Padding before background is a margin; after it, a padding (Kotlin sheet, card 9).
Lab 1 The same band in XML 1 min
res › layout › activity_main.xmlRead only
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context=".MainActivity">
<View android:id="@+id/header" android:layout_width="0dp" android:layout_height="168dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:background="#C2410C" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<ImageView android:id="@+id/imageLogo" android:layout_width="36dp" android:layout_height="36dp" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:contentDescription="Notes logo" app:layout_constraintStart_toStartOf="@id/header" app:layout_constraintTop_toTopOf="@id/header" app:srcCompat="@drawable/ic_launcher_foreground" />
<TextView android:id="@+id/textAppTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:text="Notes" android:textColor="#FFFFFF" android:textSize="30sp" app:layout_constraintStart_toStartOf="@id/imageLogo" app:layout_constraintTop_toBottomOf="@id/imageLogo" />
<TextView android:id="@+id/textCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="0" android:textColor="#FFFFFF" android:textSize="14sp" app:layout_constraintBaseline_toBaselineOf="@id/textAppTitle" app:layout_constraintStart_toEndOf="@id/textAppTitle" />
<LinearLayout android:id="@+id/composer" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="120dp" android:layout_marginEnd="32dp" android:background="#FFFFFF" android:orientation="vertical" android:padding="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@id/header">
<EditText android:id="@+id/editTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Title" android:importantForAutofill="no" android:inputType="textCapSentences" />
<EditText android:id="@+id/editBody" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:gravity="top" android:hint="Write something…" android:importantForAutofill="no" android:inputType="textMultiLine|textCapSentences" android:maxLength="140" android:minLines="3" />
<TextView android:id="@+id/textError" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="Give your note a title first." android:textSize="14sp" android:visibility="gone" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:gravity="center_vertical" android:orientation="horizontal">
<TextView android:id="@+id/textCounter" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0 / 140" android:textSize="12sp" />
<Button android:id="@+id/buttonClear" style="@style/Widget.Material3.Button.TextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" />
<Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:text="Add" /> </LinearLayout> </LinearLayout>
<TextView android:id="@+id/textListTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="24dp" android:text="YOUR NOTES" android:textSize="12sp" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/composer" />
<TextView android:id="@+id/textListHint" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="32dp" android:gravity="end" android:text="Tap to open · hold to delete" android:textSize="12sp" app:layout_constraintBaseline_toBaselineOf="@id/textListTitle" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/textListTitle" />
<ScrollView android:id="@+id/scrollNotes" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/textListHint">
<LinearLayout android:id="@+id/notesContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingStart="16dp" android:paddingEnd="16dp" android:paddingBottom="16dp" /> </ScrollView>
<TextView android:id="@+id/textEmpty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="48dp" android:gravity="center" android:text="No notes yet.\nWrite your first one above." android:textSize="16sp" android:visibility="gone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@id/scrollNotes" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. The height, 168dp.
  2. The margin: three attributes. In Compose, one padding before the paint.
  3. The colour. In Compose, background.
  4. The count on the title’s baseline. In Compose, alignByBaseline() in a Row.
  5. The card starts 120dp under the band’s top, so it overlaps. In Compose, a Box and top = 136.dp.
  6. The padding inside the card. In Compose, a padding after the paint.
Part 1 not finished, or a fresh project? Start here about 3 min
  1. The project: your own from Part 1, or a fresh one: Part 0, steps 0.2 to 0.4 (about 10 minutes).

  2. Your package name, for the code of this page:

    Without JavaScript, the code says com.yourname.notes: replace yourname by the name you chose.

  3. Kotlin: open MainActivity.kt, select all, then paste this file.

    kotlin+java › com.yourname.notes › MainActivity.ktReplace the whole file
    package com.yourname.notes
    import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.ui.Modifierimport androidx.compose.ui.tooling.preview.Previewimport com.yourname.notes.ui.theme.NotesTheme
    class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { NotesTheme { Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> Greeting( name = "Notes", modifier = Modifier.padding(innerPadding) ) } } } }}
    @Composablefun Greeting(name: String, modifier: Modifier = Modifier) { Text( text = "Hello, $name!", modifier = modifier )}
    @Preview(showBackground = true)@Composablefun GreetingPreview() { NotesTheme { Greeting("Notes") }}
  4. Restarting this part? Also delete NotesScreen.kt (right-click it, Delete…, OK) before step 2.1.

  5. ▶. You are ready when the phone shows “Hello, Notes!” at the top left.

The phone at the end of Part 1: “Hello, Notes!” in small grey letters at the top left, under the status bar.
Where you start
The phone at the end of this part: an orange band with the Android head and Notes 0, a white card over its lower edge with two grey-bordered boxes, 0 / 140, Clear and a blue-grey Add, then YOUR NOTES and its hint.
Where you arrive
  1. 2.2 the band, the logo, the title
  2. 2.3 the card over the band
  3. 2.4 the list’s title row

Before you start

By the end you can

  • stack, line up and overlap pieces with Column, Row and Box;
  • size and space a piece with a Modifier, in the right order;
  • call your own screen from the activity.
  • Kotlin kotlin+java › com.yourname.notes
  • Android Studio Split · preview
  • Emulator

No layout file this time: the whole screen is Kotlin.

Step 1 of 5

Create the screen’s file

7 min

Goal A new file, NotesScreen.kt: one composable function with an empty Column, and its preview.

  • Android Studio Project
  • Kotlin kotlin+java › com.yourname.notes › NotesScreen.kt
  1. A new Kotlin file.

    1. In the Project window, right-click kotlin+java › com.yourname.notes, the one without (test).
    2. Click NewKotlin Class/File.
    3. A New Kotlin Class/File window opens: type NotesScreen, click File in the list, then press Enter.
    4. NotesScreen.kt opens with package com.yourname.notes on line 1, and an empty line 2 under it.
    New Kotlin Class/File A dialogFull size
    The New Kotlin Class/File window: NotesScreen typed in the name field, and File selected in the list under it.
    1. The name: NotesScreen.
    2. File: a file for functions, not a class.
  2. Paste The imports of the whole part, first.

    1. On the panel, click Copy 26 lines.
    2. Click at the end of line 2, the empty line under package, press Enter, then Ctrl+V.
    3. The imports are grey: not used yet. The next steps use them; nothing should be red.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
  3. Type The function, and its preview.

    1. Click at the end of line 28, press Enter twice, then type lines 30 to 32.
    2. Line 32 ends with {}: Android Studio closed the brace for you.
    3. A list of suggestions opens as you type? Press Esc before Enter: with the list open, Enter picks a suggestion and stays on the line.
    4. Each { you type brings its }: the grey lines arrive by themselves. An empty line more or less changes nothing.
    5. Click after the last } of the file, press Enter twice, then type lines 35 to 39.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) {}}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

    The shape of Part 1’s Greeting. modifier: Modifier = Modifier: a parameter with a default value (Kotlin sheet, card 6); line 32 passes it to the Column.

Done NotesScreen.kt shows no red: one function and its preview.

Stuck? 2 symptoms · the file
  • The file holds class NotesScreen { You picked Class. Delete the class’s lines, keep package, then go on with the paste.
  • A name is red: Unresolved reference An import is missing. Paste the file below over yours.

Behind? The files at the end of this step

kotlin+java › com.yourname.notes › NotesScreen.ktReplace the whole file
package com.yourname.notes
import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
@Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) {}}
@Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

Step 2 of 5

Build the band: a Column and its Modifier

8 min

Goal In a Box, the orange band: the logo, then Notes and its count on one line.

  • Kotlin NotesScreen.kt
  • Android Studio Split · preview
  1. Type The band: a Column, sized and spaced by its Modifier.

    1. Click between the { and the } at the end of line 32, Column(…) {}, then press Enter.
    2. Type lines 33 to 41, one Modifier call per line; Esc before Enter if a list is open. The ) of line 42 arrives by itself.
    3. Press ↓, then End, and type {: its } comes by itself.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) {} } }}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

    16.dp: a size that looks the same on every screen, as in Lab 1 (card 9). The colour is written here by hand until Part 3.

  2. Paste The logo, inside the band.

    1. Click Copy 5 lines, then click between the { and the } of line 42, ) {}.
    2. Press Enter, then Ctrl+V.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) } } }}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

    painterResource reads a picture from res/drawable: Compose still uses it. The launcher’s Android head stands in for the logo until Part 3.

  3. Type The title row: Notes and its count, on one baseline.

    1. Click at the end of line 47, the ) that closes Image(, then press Enter.
    2. Type lines 48 to 53; Esc before Enter if a list is open. 30.sp is a text size (card 9).
    3. Press ↓, End, then Enter, and type lines 55 to 60. The grey lines arrive by themselves.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } } } }}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}
  4. See it in the preview.

    1. Click Split, at the top right of the editor.
    2. The preview asks for a build? Click Build & Refresh, as in Part 1.
    Preview EditorFull size
    The preview of NotesScreenPreview: an orange band with a margin around it, the Android head at its top left, then Notes in large white letters and a small 0 on the same line.

Done In the preview: an orange band, the Android head, then Notes and a small 0.

Stuck? 3 symptoms · the file
  • The band touches the preview’s edges .padding(16.dp) is not first in the chain. Move it back above .fillMaxWidth().
  • The 0 sits higher than the letters of Notes One alignByBaseline() is missing: both Texts need it.
  • A red } near the end of the file One } too many, or one missing. Compare with the file below.

Behind? The files at the end of this step

kotlin+java › com.yourname.notes › NotesScreen.ktReplace the whole file
package com.yourname.notes
import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
@Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } } } }}
@Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

Step 3 of 5

Lay the card over the band

5 min

Goal Paste the white card into the Box, after the band: it covers the band’s lower part.

  • Kotlin NotesScreen.kt
  1. Paste The card: a white Column, two fields and a row.

    1. Click Copy 37 lines, then click at the end of line 63, the } that closes the band.
    2. Press Enter, then Ctrl+V.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
    // The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } } }}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

    top = 136.dp: 16 of margin + 120, so the card starts inside the band. value = "": the fields show an empty text (Part 5). weight(1f): the counter takes the free space, like Lab 1’s 0dp.

Why A Box draws its pieces one over the other, the last on top: the card comes after the band, so it covers it.

Done In the preview: the white card over the band’s lower edge, two grey boxes, 0 / 140, Clear and Add.

Stuck? 2 symptoms · the file
  • The card sits under the band, not over it You pasted after the Box’s }: the screen’s Column stacks it. Cut it, and paste it after the band’s }, inside Box {.
  • The band covers the card The card comes before the band in the Box. Move it after the band: the last piece is on top.

Behind? The files at the end of this step

kotlin+java › com.yourname.notes › NotesScreen.ktReplace the whole file
package com.yourname.notes
import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
@Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
// The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } } }}
@Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

Step 4 of 5

Line up the list’s title

4 min

Goal Under the Box, a Row: YOUR NOTES at the left, the hint stretched to the right.

  • Kotlin NotesScreen.kt
  • Android Studio Split · preview
  1. Type The row, with YOUR NOTES.

    1. Click at the end of line 101, the } that closes the Box, then press Enter.
    2. Type the marked lines; Esc before Enter if a list is open. The grey } arrives by itself.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
    // The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } }
    // The list's title, and its hint on the right Row(modifier = Modifier.padding(start = 32.dp, top = 24.dp, end = 32.dp)) { Text(text = "YOUR NOTES", modifier = Modifier.alignByBaseline()) } }}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}
  2. Paste The hint, beside it.

    1. Click Copy 8 lines, then click at the end of line 105, the YOUR NOTES line.
    2. Press Enter, then Ctrl+V. Look at the preview.
    Preview EditorFull size
    The preview of the whole skeleton: the orange band, the white card over its lower edge with two grey boxes, 0 / 140, Clear and Add, then YOUR NOTES at the left and Tap to open · hold to delete at the right.
    kotlin+java › com.yourname.notes › NotesScreen.ktAdd the marked lines
    package com.yourname.notes
    import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
    @Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
    // The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } }
    // The list's title, and its hint on the right Row(modifier = Modifier.padding(start = 32.dp, top = 24.dp, end = 32.dp)) { Text(text = "YOUR NOTES", modifier = Modifier.alignByBaseline()) Text( text = "Tap to open · hold to delete", textAlign = TextAlign.End, modifier = Modifier .padding(start = 16.dp) .weight(1f) .alignByBaseline() ) } }}
    @Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

    weight(1f): the hint fills the free space, like Lab 1’s 0dp. TextAlign.End puts its words at the right.

Done In the preview: YOUR NOTES at the left, the hint at the right, on one line.

Stuck? 2 symptoms · the file
  • The hint sits right after YOUR NOTES Its .weight(1f) is missing: without it, the hint takes only the room of its words.
  • The row is inside the card You typed it before the Box’s }. It goes after it, in the screen’s Column.

Behind? The files at the end of this step

kotlin+java › com.yourname.notes › NotesScreen.ktReplace the whole file
package com.yourname.notes
import androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.borderimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.yourname.notes.ui.theme.NotesTheme
@Composablefun NotesScreen(modifier: Modifier = Modifier) { Column(modifier = modifier.fillMaxSize()) { Box { // The header Column( modifier = Modifier .padding(16.dp) .fillMaxWidth() .height(168.dp) .background(Color(0xFFC2410C)) .padding(start = 16.dp, top = 20.dp) ) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = "Notes logo", modifier = Modifier.size(36.dp) ) Row(modifier = Modifier.padding(top = 4.dp)) { Text( text = "Notes", color = Color.White, fontSize = 30.sp, modifier = Modifier.alignByBaseline() ) Text( text = "0", color = Color.White, modifier = Modifier .padding(start = 8.dp) .alignByBaseline() ) } }
// The composer: a white card that overlaps the header Column( modifier = Modifier .padding(start = 32.dp, top = 136.dp, end = 32.dp) .fillMaxWidth() .background(Color.White) .padding(16.dp) ) { BasicTextField( value = "", onValueChange = { }, modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) BasicTextField( value = "", onValueChange = { }, modifier = Modifier.padding(top = 8.dp).fillMaxWidth().border(1.dp, Color.LightGray).padding(12.dp) ) Row( modifier = Modifier.padding(top = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Text(text = "0 / 140", modifier = Modifier.weight(1f)) TextButton(onClick = { }, modifier = Modifier.width(88.dp)) { Text("Clear") } Button( onClick = { }, modifier = Modifier .padding(start = 4.dp) .width(88.dp) ) { Text("Add") } } } }
// The list's title, and its hint on the right Row(modifier = Modifier.padding(start = 32.dp, top = 24.dp, end = 32.dp)) { Text(text = "YOUR NOTES", modifier = Modifier.alignByBaseline()) Text( text = "Tap to open · hold to delete", textAlign = TextAlign.End, modifier = Modifier .padding(start = 16.dp) .weight(1f) .alignByBaseline() ) } }}
@Preview(showBackground = true)@Composablefun NotesScreenPreview() { NotesTheme { NotesScreen() }}

Step 5 of 5

Show your screen on the phone

8 min

Goal The activity calls NotesScreen instead of Greeting; the phone shows Lab 2’s own skeleton.

  • Kotlin kotlin+java › com.yourname.notes › MainActivity.kt
  • Emulator
  1. Type Call your screen.

    1. Open MainActivity.kt. Select lines 23 to 26, from Greeting( to its ).
    2. Type the marked line in their place.
    kotlin+java › com.yourname.notes › MainActivity.ktAdd the marked lines
    package com.yourname.notes
    import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.ui.Modifierimport androidx.compose.ui.tooling.preview.Previewimport com.yourname.notes.ui.theme.NotesTheme
    class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { NotesTheme { Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> NotesScreen(modifier = Modifier.padding(innerPadding)) } } } }}
    @Composablefun Greeting(name: String, modifier: Modifier = Modifier) { Text( text = "Hello, $name!", modifier = modifier )}
    @Preview(showBackground = true)@Composablefun GreetingPreview() { NotesTheme { Greeting("Notes") }}
  2. Paste The system bars, and Greeting gone.

    1. Click Copy the file. In MainActivity.kt, press Ctrl+A, then Ctrl+V.
    2. Your NotesScreen(…) line is in it, unchanged. Greeting and its preview are gone.
    kotlin+java › com.yourname.notes › MainActivity.ktReplace the whole file
    package com.yourname.notes
    import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.foundation.layout.WindowInsetsimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.safeDrawingimport androidx.compose.material3.Scaffoldimport androidx.compose.ui.Modifierimport com.yourname.notes.ui.theme.NotesTheme
    class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { NotesTheme { Scaffold( modifier = Modifier.fillMaxSize(), // keep the screen out of the status bar, the gesture bar and the keyboard contentWindowInsets = WindowInsets.safeDrawing ) { innerPadding -> NotesScreen(modifier = Modifier.padding(innerPadding)) } } } }}

    WindowInsets.safeDrawing: the Scaffold keeps the screen out of the status bar, the gesture bar and the keyboard. innerPadding hands that space to NotesScreen.

  3. ▶ Run 'app'.

  4. Try to write a title.

    1. On the phone, tap the first grey box, then type a letter.
    2. Nothing appears. Part 5 says why.
Lab 2’s skeleton on the phone: an orange band with the Android head and Notes 0, a white card over its lower edge with two grey-bordered boxes, 0 / 140, Clear and a blue-grey Add, then YOUR NOTES and Tap to open · hold to delete.

You should see Lab 2’s own skeleton: grey boxes without hints, Add in your wallpaper’s colour. The hints, the colours and the type sizes arrive in Part 3.

Done I see the skeleton, and the first box refuses my letters.

Stuck? 3 symptoms · the file
  • The band goes up behind the status bar’s clock NotesScreen lost modifier = Modifier.padding(innerPadding). Paste b’s file again.
  • The phone still says “Hello, Notes!” The paste did not replace the file. Press Ctrl+A before Ctrl+V, then ▶ again.
  • Add is blue or purple, not orange Normal: from Android 12, colours come from your wallpaper. Part 3 turns that off.

The file at the end of this step is b’s panel: Copy the file. NotesScreen.kt is in 2.4’s Stuck? fold.

If it doesn’t work

  • A red name Click the name and press Alt+Enter › Import. If a list opens, take the androidx.compose… line, never android.graphics, android.media, android.widget or java.lang.reflect. Almost every name red in NotesScreen.kt? 2.1’s imports were skipped: paste them once, after line 2 (2.1, action b).
  • Conflicting import: imported name '…' is ambiguous An import line is there twice: the imports were pasted again. Delete the second copy, or open the last done step’s Stuck? fold and paste its file.
  • Too many arguments for constructor(): Color or Unresolved reference White Color came from android.graphics. Its line must read import androidx.compose.ui.graphics.Color.
  • Cannot create an instance of an abstract class on Image Image came from android.media. Its line must read import androidx.compose.foundation.Image.
  • None of the following candidates is applicable on Button, with constructor(p0: Context!) Button came from android.widget. Its line must read import androidx.compose.material3.Button.
  • Classifier class Modifier does not have a companion object Modifier came from java.lang.reflect. Its line must read import androidx.compose.ui.Modifier.
  • A successful build is needed before the preview can be displayed Click Build & Refresh in the preview.

End of the part

Recap

No sixteen constraints: three containers placed the screen. A Column stacks, a Row lines up, a Box overlaps. The Modifier, read top to bottom, sizes and spaces each piece.

  • stack, line up and overlap pieces with Column, Row and Box;
  • size and space a piece with a Modifier, in the right order;
  • call your own screen from the activity.
Your app at the end of Part 2: the skeleton of the notes screen, a hard-coded orange, no hints and no style yet.
Your app now