Skip to content

Intermediate: Android basics · Android Studio and your first app · topic 21 of 50 · 30 min

Hello Compose: your first screen

Explanation

Jetpack Compose builds UI from Kotlin functions marked @Composable. setContent in the activity shows them, wrapped in your app theme. @Preview shows a composable in Android Studio without running the app. Use string resources with stringResource, log with Log.d and read crashes in Logcat.

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

kotlin
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") }
}
Close to what the Empty Activity template generates (details vary by Android Studio version).
  • 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

kotlin
// res/values/strings.xml:  <string name="welcome">Welcome, %1$s!</string>

@Composable
fun Welcome(name: String) {
    Text(stringResource(R.string.welcome, name))
}
R is a generated class with IDs for every resource. %1$s is a placeholder for the first argument.

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.

output
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)
    ...
The line with your package (MainActivity.kt:42) tells you where to look. Fix: use toIntOrNull().
Tip: Filter Logcat by your app (package:mine) and by your tag to cut through the noise from the rest of the system.

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

Describe, not modify

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.

kotlin
@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") }
Each preview shows one variation in the editor.

Practice (0/3 done)

Task 1 · Question

What does @Preview do?

Task 2 · Question

In a crash stack trace in Logcat, which line usually helps most?

Task 3 · Practice

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

Question 1

1.What does "declarative UI" mean?

Question 2

2.Which function shows Compose content in an activity?

Question 3

3.What is R in R.string.welcome?

Question 4

4.Can you call a @Composable function inside a click handler?

Read more (free)

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.

From Debugging and performance

What is the recommended modern way to build Android screens?Related

Answer: Jetpack Compose with Kotlin

Compose is Android's recommended UI toolkit.

From Kotlin and Android: the big picture

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.

From State and recomposition

Which function gets a ViewModel inside a composable (without Hilt)?Related

Answer: viewModel()

viewModel() returns the instance scoped to the screen.

From ViewModel and MVVM

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

  1. This is the last topic of “Android Studio and your first app”. The module exam comes next.
  2. Module 5 exam: Android Studio and your first app · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Jetpack Compose UI. 50 topics left in the course before the final exam.

← Previous: Activities and the lifecycle