First Compose App
Android Development Fundamentals with Compose
Course 4 · Chapter 1 · Your First App, Built in Code
Older Android courses start by drawing a screen in an XML file. This one does not. With Jetpack Compose the screen is Kotlin code: you write composable functions, each one describing a piece of UI, and
Android draws what they describe. By the end of this chapter you will have created a project, found your way around its files, read the generated MainActivity.kt line by line, changed what it shows and run it.
res/layout folder).
Checked in the docs (one web search, summaries only): that Empty Activity is the recommended starting point and that its MainActivity calls setContent with a Scaffold and a Greeting composable. Not verified: the exact
code your Rabbit 2 template generates (the docs I found did not confirm it, so the listing below is a shape; compare it with your own file), and all code in this chapter is unrun, because there is no Kotlin compiler on the machine the course was written on.
Create the Project
The wizard is covered screen by screen in Android Studio 2: The New UI 3, so here is only the path. From the Welcome screen choose New Project (or File > New > New Project), pick Empty Activity (the Compose one: the Views templates have different names), then fill in the second screen:
| Field | What to enter |
|---|---|
| Name | ComposeHello (any name; this course's examples use it). |
| Package name | Left as suggested, for example com.example.composehello. |
| Save location | A folder of your choice. |
| Minimum SDK | The default on the install was API 24 (Android 7.0). Accept it. |
Press Finish. The first Gradle sync downloads what the project needs and can take a few minutes; wait for it to end before running anything (Android Studio 2: The New UI 4 explains what sync does).
What the Project Contains
In the Android view, with a few files omitted, the project looks like this (checked against a generated project):
| File | What it is for |
|---|---|
AndroidManifest.xml | Declares the app to Android: its name, icon and its activities. MainActivity is listed here as the screen that opens when you tap the app. |
MainActivity.kt | The entry point: the one Activity, which puts a Compose UI on screen. |
ui/theme/ | Color.kt, Type.kt and Theme.kt: the app's colours, fonts and the theme function that wraps your UI (Chapter 8). |
res/ | Resources: icons, strings, colours. There is no layout folder, because there are no XML layouts. |
app/build.gradle.kts | The module's build file: dependencies and settings, in Kotlin DSL. |
libs.versions.toml | The version catalog: one place for library versions. |
Reading MainActivity.kt
Compare your own file with this listing. It shows what the Empty Activity template is documented to generate; small differences (for example an extra enableEdgeToEdge() line, or different imports) are normal.
| Part | What it does |
|---|---|
ComponentActivity | The base class for an Activity that can host Compose. |
onCreate | Called when the screen is created. Chapter 2 covers the whole lifecycle. |
setContent { ... } | Replaces setContentView and a layout file: the block inside is the screen. |
ComposeHelloTheme | Your app's theme from ui/theme/Theme.kt; everything inside takes its colours and fonts. |
Scaffold | A Material 3 layout skeleton (top bar, bottom bar and so on). It hands you innerPadding so your content does not slide under system bars. |
Greeting | A composable function of your own: it takes data (name) and describes UI. |
Modifier | A chain of instructions about size, padding, position and behaviour. Chapter 3 covers it. |
GreetingPreview | Draws Greeting inside Android Studio without running the app (Android Studio 2: The New UI 5). |
What Makes a Function Composable
- It is marked
@Composable, which tells the Compose compiler that it describes UI. - It is named in PascalCase (
Greeting, notgreeting), like a class: that is the Compose convention. - It usually returns nothing. It does not hand back a view; calling it emits UI into the screen.
- It can only be called from another composable (or from
setContent), not from ordinary code. - It takes a
modifier: Modifier = Modifierparameter as its first optional parameter, so callers can adjust its size and position.
Run It
Start an emulator or connect a phone (Android Studio 2: The New UI 6 shows how), choose it in the toolbar's device selector and press Run. You should see the greeting text in the top left. Press Run again after each change to see it. The preview shows the same thing without a device; it is faster for small visual changes.
Compared with the XML Way
| Views and XML (Android Development Fundamentals) | Compose | |
|---|---|---|
| Where the screen is defined | An XML layout file such as activity_main.xml | Kotlin functions |
| How the Activity shows it | setContentView(R.layout.activity_main) | setContent { ... } |
| Changing what is shown | Find a view by id and change it | Change the data; the UI is redrawn from it (Chapter 4) |
| Building blocks | View classes: TextView, Button | Composables: Text, Button |
| Seeing it without running | Layout Editor | @Preview |
Try It Yourself
Create a project called ComposeHello with the Empty Activity template, wait for the sync, and write down the files and folders you see in the Android view. Does your MainActivity.kt match the listing above? List any differences.
Change Greeting so it shows two lines of text: Hello, <name>! and Welcome to Compose. Check the preview, then run the app on a device.
Write your own composable ProfileCard(name: String, role: String) that shows the two values, give it a @Preview, then call it from setContent instead of Greeting.
Checked in the docs (one web search, summaries only): Empty Activity as the recommended Compose starting point; a template
MainActivity with setContent, Scaffold, Greeting and a preview; enableEdgeToEdge being added by hand in the codelabs I found.Not verified: the exact code and imports the Rabbit 2 template generates; the Compose conventions listed under "What Makes a Function Composable" (general knowledge); every code listing and solution in this chapter (unrun, not compiled); the exercises (not run by me). If your generated file differs, trust your file and tell me what it contains.
Sources: Create your first Android app (developer.android.com), Quick start, Jetpack Compose (developer.android.com), Create a project (developer.android.com).
rememberSaveable keeps data across a rotation.