Mehdi TmimiMobile DevelopmentMobile

Mobile Development Lab 2 Part 1 · parts 0–7

A screen is a function

Lab 1 needed three files for a screen: a layout, a class, a manifest line. Look for the layout here.

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.

kotlin+java › com.yourname.notes › MainActivity.ktRead only
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") }}
  1. setContent { } replaces setContentView(R.layout.…): what is inside the braces is the screen.
  2. NotesTheme: the app’s colours and type, for everything inside it (Part 3).
  3. Scaffold: keeps its content clear of the system bars, and hands over that room as innerPadding.
  4. Greeting(…): a call of your own function, with a name.
  5. @Composable: this function says what to show. It returns nothing: it draws.
  6. @Preview: Android Studio draws this function itself, without the phone (step 1.2).

setContent { } the activity’s screen

NotesTheme { } colours and type

Scaffold { } clear of the system bars

Greeting("Android")

Hello Android!

The calls of lines 20 to 23, drawn: each frame sits inside the call around it, and the phone shows the text of the innermost one.
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

kotlin+java › com.yourname.notes › MainActivity.javaRead only
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

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">
<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

  1. Start Android Studio: it reopens your Compose project by itself. If the Welcome window shows instead: ProjectsNotes, the one whose path ends in NotesCompose.
  2. 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.
  3. 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
  1. The project: your NotesCompose project from Part 0. If it does not build, create a fresh one: Part 0, steps 0.2 to 0.4.

  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.

The phone at the end of Part 0: the Compose template’s app, “Hello Android!” in small type at the top left, under the status bar.
Where you start

Before you start

By the end you can

  • say where a Compose screen lives, and what setContent, @Composable and @Preview do;
  • 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

3 min

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
  1. The files of a Compose project.

    1. In Project, keep the Android display. Open app › kotlin+java › the plain com.yourname.notes, each with its arrow ›.
    2. Find MainActivity.kt: the activity and its screen, in Kotlin.
    3. Open ui.theme beside it: Color.kt, Theme.kt, Type.kt. Lab 1’s colors.xml and themes.xml, now Kotlin.
    4. Open res: drawable, mipmap, values, xml. No layout folder.
    Project · Android Project window · leftFull size
    The Project window in the Android display: app with manifests, kotlin+java holding com.yourname.notes with ui.theme (Color.kt, Theme.kt, Type.kt) and MainActivity.kt, then res with drawable, mipmap, values and xml, and no layout folder.
    1. MainActivity.kt: the class and its screen.
    2. ui.theme: the theme, three Kotlin files.
    3. res: no layout folder.
  2. The manifest line, already written.

    1. Open manifests, then double-click AndroidManifest.xml.
    2. 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>
    1. android:name=".MainActivity": the class Android opens, MainActivity.kt.
    2. MAIN and LAUNCHER (lines 21 and 23): the icon opens it. In Lab 1 you typed these lines in Part 2; here the wizard wrote them.
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

Gradle Scripts › build.gradle.kts (Module :app)Read only
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

Gradle Scripts › build.gradle.kts (Module :app)Read only
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 = true turns 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 had appcompat and material as 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.java and a layout folder in the tree This is Lab 1’s project. Click FileClose Project, then in Welcome open the Notes whose path ends in NotesCompose.
  • No manifests folder 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

4 min

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
  1. The file, beside its preview.

    1. In Project, double-click MainActivity.kt, unless its tab is already open.
    2. At the top right of the editor, click Split, the middle of the three icons. Already highlighted? Leave it.
    3. Read the right half: A successful build is needed before the preview can be displayed.
    MainActivity.kt · Split Editor · centreFull size
    The preview half of the editor in Split: at the top right the three icons with Split highlighted; below, a warning, A successful build is needed before the preview can be displayed, and the link Build & Refresh… (Ctrl+Shift+F5).
    1. Split: the code and its preview, side by side.
    2. Build & Refresh…: the next action.
  2. One build, then the preview.

    1. Click Build & Refresh… under the message, or press Ctrl+Shift+F5.
    2. Wait: Build opens at the bottom and ends with BUILD SUCCESSFUL.
    3. Read the card GreetingPreview: Hello Android!, with Up-to-date above it.
    MainActivity.kt · Split Editor · centreFull size
    The preview half after the build: Up-to-date at the top right, and a card titled GreetingPreview showing Hello Android! in black on white.
    1. GreetingPreview: the function of line 43, drawn.
    2. 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 exist Android Studio reused a Gradle process left by VS Code. Close VS Code, type .\gradlew --stop in 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

4 min

Goal Make the screen say Hello, Notes!: a comma in the string, then the name in both calls.

  • Kotlin MainActivity.kt · Split
  1. Type The greeting, then the two names.

    1. Line 36: click right after Hello, inside the quotes, then type a comma. It reads "Hello, $name!".
    2. Line 24: double-click Android, between the quotes, then type Notes.
    3. Line 45: double-click Android, then type Notes. The grey name = before it is Android Studio’s hint: never type it.
    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 -> 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. $name puts the value of name in the string: Kotlin sheet, card 1.

  2. The new greeting, in the preview.

    1. Look at the card GreetingPreview: it follows your typing by itself, Hello, Notes!
    2. Still the old text, under Out of date? Press Ctrl+Shift+F5.
    MainActivity.kt · Split Editor · centreFull size
    The preview half after the edits: the card GreetingPreview shows Hello, Notes! in black on white.
    1. 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

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") }}

Step 4 of 4

Run the app on the phone

2 min

Goal Run the app and see the phone show Hello, Notes!, the call of line 24.

  • Android Studio Run
  • Emulator Running Devices
  1. Your app, on the phone.

    1. In the toolbar, check that the device menu says Pixel 7.
    2. Click ▶ Run 'app', or press Shift+F10. Already running? Click ↻ Rerun in its place.
The emulator: “Hello, Notes!” in small type at the top left, just under the status bar; the rest of the screen is empty.

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.

The top left of the phone at real size: the clock 9:41, and under it Hello, Notes!
The top left, at real size

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, @Composable and @Preview do;
  • see a composable in the preview, without the phone;
  • change the screen in Kotlin, then run it.
Your app at the end of Part 1: “Hello, Notes!” at the top left of the phone.
Your app now