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.

What was checked, and how: read this first
Checked on the install (Android Studio Rabbit 2, from earlier work): the new-project wizard's Empty Activity option, the default minimum SDK (API 24), and the list of files a generated Compose project contains (no 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:

FieldWhat to enter
NameComposeHello (any name; this course's examples use it).
Package nameLeft as suggested, for example com.example.composehello.
Save locationA folder of your choice.
Minimum SDKThe 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):

ComposeHello/ ├── app/ │ ├── build.gradle.kts │ └── src/main/ │ ├── AndroidManifest.xml │ ├── java/com/example/composehello/ │ │ ├── MainActivity.kt │ │ └── ui/theme/ │ │ ├── Color.kt │ │ ├── Theme.kt │ │ └── Type.kt │ └── res/ (no layout folder) ├── gradle/libs.versions.toml ├── build.gradle.kts └── settings.gradle.kts
FileWhat it is for
AndroidManifest.xmlDeclares 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.ktThe 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.ktsThe module's build file: dependencies and settings, in Kotlin DSL.
libs.versions.tomlThe 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.

// MainActivity.kt (the shape of the generated file; yours may differ slightly) package com.example.composehello import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import com.example.composehello.ui.theme.ComposeHelloTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ComposeHelloTheme { Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> Greeting(name = "Android", modifier = Modifier.padding(innerPadding)) } } } } } @Composable fun Greeting(name: String, modifier: Modifier = Modifier) { Text(text = "Hello $name!", modifier = modifier) } @Preview(showBackground = true) @Composable fun GreetingPreview() { ComposeHelloTheme { Greeting("Android") } }
PartWhat it does
ComponentActivityThe base class for an Activity that can host Compose.
onCreateCalled when the screen is created. Chapter 2 covers the whole lifecycle.
setContent { ... }Replaces setContentView and a layout file: the block inside is the screen.
ComposeHelloThemeYour app's theme from ui/theme/Theme.kt; everything inside takes its colours and fonts.
ScaffoldA Material 3 layout skeleton (top bar, bottom bar and so on). It hands you innerPadding so your content does not slide under system bars.
GreetingA composable function of your own: it takes data (name) and describes UI.
ModifierA chain of instructions about size, padding, position and behaviour. Chapter 3 covers it.
GreetingPreviewDraws 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, not greeting), 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 = Modifier parameter 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 definedAn XML layout file such as activity_main.xmlKotlin functions
How the Activity shows itsetContentView(R.layout.activity_main)setContent { ... }
Changing what is shownFind a view by id and change itChange the data; the UI is redrawn from it (Chapter 4)
Building blocksView classes: TextView, ButtonComposables: Text, Button
Seeing it without runningLayout Editor@Preview
Compared with the Classic UI course
Android Development Fundamentals 1 starts with an IDE tour and a first app built from XML. The IDE has changed (see Android Studio 2: The New UI 1, 3 and 4), and a new project is now a Compose project, so this chapter replaces both the tour and the first-app steps.

Try It Yourself

Exercise 1

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.

📄 View solution
Exercise 2

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.

📄 View solution
Exercise 3

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.

📄 View solution
Not Yet Verified
Checked on the install: the Empty Activity option, API 24 as the default minimum SDK, and the generated project's file list.
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).
Coming up
Chapter 2 covers Activities and the lifecycle: what happens when the screen is created, paused or rotated, and how rememberSaveable keeps data across a rotation.