Intermediate: Android basics · Android Studio and your first app · topic 21 of 50 · 30 min
Hello Compose: your first screen
Explanation
Declarative UI
In the old Android way, you designed screens in XML files and then changed them from code ("find this TextView, set its text"). Jetpack Compose is declarative: you write Kotlin functions that describe what the screen should look like for the current data, and Compose updates the screen when the data changes. Less code, fewer bugs where the screen and the data disagree.
What the template gives you
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
HelloIndiaTheme {
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() {
HelloIndiaTheme { Greeting("Android") }
}- enableEdgeToEdge() lets the app draw behind the status and navigation bars; Scaffold's innerPadding keeps your content clear of them. Apps targeting Android 15 (API 35) are edge-to-edge by default.
- setContent { } replaces the old setContentView: it shows Compose content.
- HelloIndiaTheme applies your colours and fonts (Material 3, module 9).
- @Composable marks a function that describes UI. Composables are named with a capital letter, like classes.
- Modifier customises size, padding, clicks and more (module 6). Passing modifier as a parameter is a convention that keeps composables flexible.
@Preview
A function marked @Preview (and @Composable) is drawn in the Split or Design view of the editor, without installing the app. You can have several previews for different states, sizes, fonts or dark mode. Previews make UI work much faster, but they cannot run real network or database code, so pass them sample data.
String resources
// res/values/strings.xml: <string name="welcome">Welcome, %1$s!</string>
@Composable
fun Welcome(name: String) {
Text(stringResource(R.string.welcome, name))
}Logcat and crashes
Logcat is the window in Android Studio that shows log messages from the device. Write your own with Log.d(TAG, "message"). When the app crashes, Logcat shows a stack trace: the exception type, the message, and the chain of function calls. Read it from the top: find the first line that mentions your own package and file, and click it to jump to the code.
FATAL EXCEPTION: main
Process: com.example.helloindia, PID: 12345
java.lang.NumberFormatException: For input string: "12a"
at java.lang.Integer.parseInt(Integer.java:...)
at com.example.helloindia.MainActivityKt.parseQty(MainActivity.kt:42)
...Common mistakes
- Calling a @Composable function from a normal function or a click handler: "@Composable invocations can only happen from the context of a @Composable function". Change state instead, and let the UI react.
- Naming composables in lowercase. The convention is PascalCase (Greeting, ProfileCard).
- Previews that call real repositories and fail to render. Pass sample data.
- Ignoring the stack trace and guessing. The answer is usually in the first lines that mention your code.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Hello Compose: your first screen · 1/6
Jetpack Compose is declarative. You describe what the screen should look like for the current data.
Prefer a video by a teacher? Find free videos on “jetpack compose first app tutorial preview setContent” on YouTube ↗
In real life: A festival greeting card screen
A small business owner wants an app screen that greets customers with the current festival's name. The developer writes a FestivalGreeting composable that takes the festival name and a colour, previews it for Diwali, Eid and Christmas side by side in Android Studio, and ships it without running the app dozens of times on a phone.
@Composable
fun FestivalGreeting(festival: String, modifier: Modifier = Modifier) {
Column(modifier.padding(24.dp)) {
Text("Happy $festival!", style = MaterialTheme.typography.headlineMedium)
Text("From all of us at Sharma Sweets")
}
}
@Preview(showBackground = true)
@Composable
fun DiwaliPreview() { FestivalGreeting("Diwali") }Practice (0/3 done)
What does @Preview do?
In a crash stack trace in Logcat, which line usually helps most?
In HelloIndia, write a composable ProfileCard(name: String, city: String) that shows two lines of text with padding, use string resources for the labels, add two @Preview functions with different data, and log a message in onCreate. Then cause a crash on purpose ("abc".toInt()), read the Logcat stack trace, and fix it with toIntOrNull.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Setting up Android Studio, an emulator and your phoneAndroid Studio and your first app · same module
- Debugging and performanceTesting Android apps
- State and recompositionJetpack Compose UI
- ViewModel and MVVMNavigation and app architecture
- Compose UI testsTesting Android apps
- Composables and modifiersJetpack Compose UI
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does "declarative UI" mean?
Answer: You describe the UI for the current state and the framework updates it
Compose re-runs composables when state changes.
Which function shows Compose content in an activity?
Answer: setContent
setContent { } hosts the composables.
What is R in R.string.welcome?
Answer: A generated class with IDs for resources
The build generates R with resource IDs.
Can you call a @Composable function inside a click handler?
Answer: No; change state and let the UI recompose
Composables can only be called from other composables.
Which is a common cause of janky Compose screens?Related
Answer: Heavy work like sorting or formatting inside composable bodies
Move work to the ViewModel or remember it.
What is the recommended modern way to build Android screens?Related
Answer: Jetpack Compose with Kotlin
Compose is Android's recommended UI toolkit.
Why should composables avoid side effects like network calls in their body?Related
Answer: They may run many times and in any order
Recomposition can happen often.
Which function gets a ViewModel inside a composable (without Hilt)?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Android Studio and your first app”. The module exam comes next.
- Module 5 exam: Android Studio and your first app · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Jetpack Compose UI. 50 topics left in the course before the final exam.