> Documentation index: https://developer.android.com/develop/ui/llms.txt

Tutorial

# Jetpack Compose Tutorial

- 4 lessons
- [Get set up \>](https://developer.android.com/develop/ui/compose/setup.md.txt)

Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose
simplifies and accelerates UI development on Android with less code, powerful tools,
and intuitive Kotlin APIs.


In this tutorial, you'll build a simple UI component with declarative functions. You won't
be editing any XML layouts or using the Layout Editor. Instead, you will call composable
functions to define what elements you want, and the Compose compiler will do the rest.
![Full Preview](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/00-hero-device.png)

- 4 lessons
- [Get setup \>](https://developer.android.com/develop/ui/compose/setup.md.txt)
![Full Preview](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/hero.png) [](https://developer.android.com/develop/ui/compose/tutorial.md.txt#top_of_page)

## Lesson 1: Composable functions


Jetpack Compose is built around composable functions. These functions let you define your
app's UI programmatically by describing how it should look and providing data dependencies,
rather than focusing on the process of the UI's construction (initializing an element,
attaching it to a parent, etc.). To create a composable function, just add the
`@Composable` annotation to the function name.
![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-01.svg)

#### Add a text element


To begin, download the most recent version of [Android Studio](https://developer.android.com/studio.md.txt), and create an app by selecting **New Project** , and under the
**Phone and Tablet** category, select **Empty Activity** .
Name your app **ComposeTutorial** and click **Finish**. The default
template already contains some Compose elements, but in this tutorial you will build it up step
by step.


First, display a "Hello world!" text by adding a text element inside the
`onCreate` method. You do this by defining a content
block, and calling the [`Text`](https://developer.android.com/reference/kotlin/androidx/compose/material3/package-summary.md.txt#Text(kotlin.String,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.font.FontStyle,androidx.compose.ui.text.font.FontWeight,androidx.compose.ui.text.font.FontFamily,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.style.TextDecoration,androidx.compose.ui.text.style.TextAlign,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.style.TextOverflow,kotlin.Boolean,kotlin.Int,kotlin.Function1,androidx.compose.ui.text.TextStyle)) composable function. The
`setContent` block defines the activity's layout where
composable functions are called. Composable functions can only be called from other composable
functions.


Jetpack Compose uses a Kotlin compiler plugin to transform these composable functions into the
app's UI elements. For example, the `Text` composable
function that is defined by the Compose UI library displays a text label on the screen.

```kotlin
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Text

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Text("Hello world!")
        }
    }
}
  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-02.png)

#### Define a composable function


To make a function composable, add the `@Composable` annotation.
To try this out, define a `MessageCard` function which is
passed a name, and uses it to configure the text element.

```kotlin
// ...
import androidx.compose.runtime.Composable

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MessageCard("Android")
        }
    }
}

@Composable
fun MessageCard(name: String) {
    Text(text = "Hello $name!")
}

  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-03.png)

#### Preview your function in Android Studio


The `@Preview` annotation lets you preview your composable functions within Android
Studio without having to build and install the app to an Android device or emulator. The
annotation must be used on a composable function that does not take in parameters. For this
reason, you can't preview the `MessageCard` function
directly. Instead, make a second function named
`PreviewMessageCard`, which calls
`MessageCard` with an appropriate parameter. Add the
`@Preview` annotation before
`@Composable`.

```kotlin
// ...
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun MessageCard(name: String) {
    Text(text = "Hello $name!")
}

@Preview
@Composable
fun PreviewMessageCard() {
    MessageCard("Android")
}
  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-03.png)
Rebuild your project. The app itself doesn't change, since the new
`PreviewMessageCard` function isn't called anywhere,
but Android Studio adds a preview window which you can expand by clicking on the split
(design/code) view. This window shows a preview of the UI elements created by composable
functions marked with the `@Preview` annotation. To update
the previews at any time, click the refresh button at the top of the preview window.
![Preview of a composable function in Android Studio](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-04.png)

```kotlin
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Text

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Text("Hello world!")
        }
    }
}
  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-02.png)

```kotlin
// ...
import androidx.compose.runtime.Composable

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MessageCard("Android")
        }
    }
}

@Composable
fun MessageCard(name: String) {
    Text(text = "Hello $name!")
}

  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-03.png)

```kotlin
// ...
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun MessageCard(name: String) {
    Text(text = "Hello $name!")
}

@Preview
@Composable
fun PreviewMessageCard() {
    MessageCard("Android")
}
  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-03.png) ![Preview of a composable function in Android Studio](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson1-04.png) [](https://developer.android.com/develop/ui/compose/tutorial.md.txt#top_of_page)

## Lesson 2: Layouts


UI elements are hierarchical, with elements contained in other elements. In Compose, you
build a UI hierarchy by calling composable functions from other composable functions.
![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson2-01.svg)

#### Add multiple texts


So far you've built your first composable function and preview! To discover more Jetpack Compose
capabilities, you're going to build a simple messaging screen containing a list of messages that
can be expanded with some animations.


Start by making the message composable richer by displaying the name of its author and a
message content. You need to first change the composable parameter to accept a
`Message` object instead of a
`String`, and add another
`Text` composable inside the
`MessageCard` composable. Make sure to update the preview
as well.

```kotlin
// ...

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MessageCard(Message("Android", "Jetpack Compose"))
        }
    }
}

data class Message(val author: String, val body: String)

@Composable
fun MessageCard(msg: Message) {
    Text(text = msg.author)
    Text(text = msg.body)
}

@Preview
@Composable
fun PreviewMessageCard() {
    MessageCard(
        msg = Message("Lexi", "Hey, take a look at Jetpack Compose, it's great!")
    )
}

  
```
show preview hide preview ![](https://developer.android.com/static/develop/ui/compose/images/compose-tutorial/lesson2-02.png)
This code creates two text elements inside the content view. However, since you haven't provided
any inform