The idea
No layout: a function says what the screen shows
Goal Find where a Compose screen lives, see it without the phone, change its text in Kotlin, then run it.
In Lab 1, setContentView loaded an XML layout. Here setContent { } calls Kotlin functions, and each call draws inside the one around it. The braces after a call: Kotlin sheet, card 8.
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 = "Android", modifier = Modifier.padding(innerPadding) ) } } } }}
@Composablefun Greeting(name: String, modifier: Modifier = Modifier) { Text( text = "Hello $name!", modifier = modifier )}
@Preview(showBackground = true)@Composablefun GreetingPreview() { NotesTheme { Greeting("Android") }}setContent { }replacessetContentView(R.layout.…): what is inside the braces is the screen.NotesTheme: the app’s colours and type, for everything inside it (Part 3).Scaffold: keeps its content clear of the system bars, and hands over that room asinnerPadding.Greeting(…): a call of your own function, with a name.@Composable: this function says what to show. It returns nothing: it draws.@Preview: Android Studio draws this function itself, without the phone (step 1.2).
1setContent { } the activity’s screen
2NotesTheme { } colours and type
3Scaffold { } clear of the system bars
4Greeting("Android")
Hello Android!
Lab 1 The same screen, in Java and XML 1 min
Lab 1 split the screen: the layout listed the views, setContentView turned them into objects, Java changed them later. Lab 2 keeps one Kotlin file.
Lab 1 · Part 2 · the class
package com.yourname.notes;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}Lab 1 · Part 2 · the layout
<?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">
<TextView android:id="@+id/textHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, Notes!" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>Start of session
- Start Android Studio: it reopens your Compose project by itself. If the Welcome window shows instead: ProjectsNotes, the one whose path ends in
NotesCompose. - Start the phone from the project: Device Manager (the phone icon on the window’s right edge, or ViewTool WindowsDevice Manager), ▶ on Pixel 7. It appears in Running Devices.
- Wait until indexing ends (the bar at the bottom) before you type in a file.
Part 0 not finished, or a fresh project? Start here a fresh project: about 10 min
The project: your
NotesComposeproject from Part 0. If it does not build, create a fresh one: Part 0, steps 0.2 to 0.4.Your package name, for the code of this page:
Without JavaScript, the code says com.yourname.notes: replace yourname by the name you chose.
Before you start
By the end you can
- say where a Compose screen lives, and what
setContent,@Composableand@Previewdo; - see a composable in the preview, without the phone;
- change the screen in Kotlin, then run it.
- Android Studio Project · Android
- Kotlin MainActivity.kt · Split
- Emulator Running Devices
Step 1 of 4
Find the screen in the project
Goal See what the template wrote: a Kotlin file, a theme in Kotlin, no layout folder, and the manifest line.
- Android Studio Project · Android
- Manifest manifests › AndroidManifest.xml
-
The files of a Compose project.
- In Project, keep the Android display. Open
app›kotlin+java› the plaincom.yourname.notes, each with its arrow ›. - Find
MainActivity.kt: the activity and its screen, in Kotlin. - Open
ui.themebeside it:Color.kt,Theme.kt,Type.kt. Lab 1’scolors.xmlandthemes.xml, now Kotlin. - Open
res:drawable,mipmap,values,xml. Nolayoutfolder.
Project · Android Project window · leftFull size
MainActivity.kt: the class and its screen.ui.theme: the theme, three Kotlin files.res: nolayoutfolder.
- In Project, keep the Android display. Open
-
The manifest line, already written.
- Open
manifests, then double-clickAndroidManifest.xml. - Find lines 15 and 21 in your editor, then read the two notes under the panel.
manifests › AndroidManifest.xmlRead only<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Notes"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.Notes" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>android:name=".MainActivity": the class Android opens,MainActivity.kt.MAINandLAUNCHER(lines 21 and 23): the icon opens it. In Lab 1 you typed these lines in Part 2; here the wizard wrote them.
- Open
Go deeper Where Compose comes from: the app’s Gradle file optional · 2 min
In Gradle Scripts, open build.gradle.kts (Module :app). Four lines bring Compose; Lab 1 had view libraries instead.
Lab 2 · Compose
plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.compose)}
android { namespace = "com.yourname.notes" compileSdk { version = release(37) }
defaultConfig { applicationId = "com.yourname.notes" minSdk = 24 targetSdk = 37 versionCode = 1 versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }
buildTypes { release { optimization { enable = true packageScope = setOf("androidx.**", "kotlin.**", "kotlinx.**") } } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } buildFeatures { compose = true }}
dependencies { implementation(platform(libs.androidx.compose.bom)) implementation(libs.androidx.activity.compose) implementation(libs.androidx.compose.material3) implementation(libs.androidx.compose.ui) implementation(libs.androidx.compose.ui.graphics) implementation(libs.androidx.compose.ui.tooling.preview) implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) testImplementation(libs.junit) androidTestImplementation(platform(libs.androidx.compose.bom)) androidTestImplementation(libs.androidx.compose.ui.test.junit4) androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.junit) debugImplementation(libs.androidx.compose.ui.test.manifest) debugImplementation(libs.androidx.compose.ui.tooling)}Lab 1 · views
plugins { alias(libs.plugins.android.application)}
android { namespace = "com.yourname.notes" compileSdk { version = release(37) }
defaultConfig { applicationId = "com.yourname.notes" minSdk = 24 targetSdk = 37 versionCode = 1 versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }
buildTypes { release { optimization { enable = true packageScope = setOf("androidx.**", "kotlin.**", "kotlinx.**") } } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 }}
dependencies { implementation(libs.appcompat) implementation(libs.material) implementation("androidx.constraintlayout:constraintlayout:2.2.2") testImplementation(libs.junit) androidTestImplementation(libs.espresso.core) androidTestImplementation(libs.ext.junit)}- Line 3: the Compose compiler plugin, for Kotlin. Line 35:
compose = trueturns Compose on. - Line 40: the BOM, one version for all the Compose libraries. Line 42:
material3, Material’s buttons and colours as functions, where Lab 1 hadappcompatandmaterialas views.
Done I see MainActivity.kt and ui.theme, and no layout folder in res.
Stuck? 4 symptoms
- The phone opened in its own window, not in Running Devices You started it from the Welcome window. Close the phone’s window, then start the phone from the project’s Device Manager.
MainActivity.javaand alayoutfolder in the tree This is Lab 1’s project. Click FileClose Project, then in Welcome open the Notes whose path ends inNotesCompose.- No
manifestsfolder in the tree The menu at the top of Project says Project, which shows the real folders. Pick Android. - The captures say
yourname, your tree does not Normal: your package carries your own name. Type it in the package field of the box at the top (Part 0 not finished, or a fresh project?).
No file changes in this step.
Step 2 of 4
Draw the screen in the preview
Goal Open MainActivity.kt in Split, build once, and see GreetingPreview draw Hello Android! without the phone.
- Kotlin kotlin+java › com.yourname.notes › MainActivity.kt · Split
-
The file, beside its preview.
- In Project, double-click
MainActivity.kt, unless its tab is already open. - At the top right of the editor, click Split, the middle of the three icons. Already highlighted? Leave it.
- Read the right half: A successful build is needed before the preview can be displayed.
MainActivity.kt · Split Editor · centreFull size
- Split: the code and its preview, side by side.
- Build & Refresh…: the next action.
- In Project, double-click
-
One build, then the preview.
- Click Build & Refresh… under the message, or press Ctrl+Shift+F5.
- Wait: Build opens at the bottom and ends with
BUILD SUCCESSFUL. - Read the card GreetingPreview: Hello Android!, with Up-to-date above it.
MainActivity.kt · Split Editor · centreFull size
- GreetingPreview: the function of line 43, drawn.
- Up-to-date: the preview matches the code.
Why The preview runs the function marked @Preview, GreetingPreview, which calls Greeting("Android"). No install, no phone: Android Studio draws it.
Done I see Hello Android! in the card GreetingPreview.
Stuck? 2 symptoms
jlink executable … does not existAndroid Studio reused a Gradle process left by VS Code. Close VS Code, type.\gradlew --stopin Android Studio’s Terminal, press Enter, then Build & Refresh.- A balloon: Build Analyzer detected new build performance issues Not an error. Close it.
No file changes in this step.
Step 3 of 4
Change the greeting in Kotlin
Goal Make the screen say Hello, Notes!: a comma in the string, then the name in both calls.
- Kotlin MainActivity.kt · Split
-
Type The greeting, then the two names.
- Line 36: click right after
Hello, inside the quotes, then type a comma. It reads"Hello, $name!". - Line 24: double-click
Android, between the quotes, then typeNotes. - Line 45: double-click
Android, then typeNotes. The greyname =before it is Android Studio’s hint: never type it.
kotlin+java › com.yourname.notes › MainActivity.ktAdd the marked linespackage 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") }}The three marked lines change; nothing moves.
$nameputs the value ofnamein the string: Kotlin sheet, card 1. - Line 36: click right after
-
The new greeting, in the preview.
- Look at the card GreetingPreview: it follows your typing by itself, Hello, Notes!
- Still the old text, under Out of date? Press Ctrl+Shift+F5.
MainActivity.kt · Split Editor · centreFull size
- GreetingPreview: Hello, Notes!, from line 45.
Why One function, two calls: line 45 is the call the preview runs, line 24 the call the phone runs.
Done I see Hello, Notes! in the preview, and "Notes" on lines 24 and 45.
Stuck? 2 symptoms · the file
- Line 36 turns red The comma landed outside the quotes. Press Ctrl+Z, then click inside the quotes, right after
Hello. - The preview still says Hello, Android! Line 45 still says
"Android": the preview runs that call, not line 24’s.
The files at the end of this step
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") }}Step 4 of 4
Run the app on the phone
Goal Run the app and see the phone show Hello, Notes!, the call of line 24.
- Android Studio Run ▶
- Emulator Running Devices
-
Your app, on the phone.
- In the toolbar, check that the device menu says Pixel 7.
- Click ▶ Run 'app', or press Shift+F10. Already running? Click ↻ Rerun in its place.

You should see Hello, Notes! in small type at the top left, under the clock. No layout and no XML drew it: a function did.

Done I see Hello, Notes! at the top left of the phone.
Stuck? 2 symptoms
- “Waiting for all target devices to come online” The phone is still booting: wait.
- The phone still says Hello Android! Line 24 still says
"Android", or the run did not end. Check line 24, then click ↻ Rerun.
No file changes in this step: the project is as at the end of step 1.3.
If it doesn’t work
- “Kotlin version 2.4.0 is available”, or any other version update Ignore it: the lab is built on the versions the wizard writes.
- “New Minor Gradle Version Available”, in Build Ignore it: the lab is tested with this Gradle.
Now you If you have time
Your name on the phone only
You want Hello, Sara! on the phone and Hello, Notes! in the preview. Which line do you change? Say it; change nothing.
See the answer
Line 24, the call inside setContent. Line 45 is the preview’s call. Keep "Notes" in both: Part 2 starts from there.
End of the part
Recap
There is no layout: the screen is a Kotlin function. setContent calls it on the phone, @Preview calls it in Android Studio, and the wizard wrote the manifest line.
- say where a Compose screen lives, and what
setContent,@Composableand@Previewdo; - see a composable in the preview, without the phone;
- change the screen in Kotlin, then run it.

