Jetpack Compose: Getting Started

Aug 1 2023 · Kotlin 1.8.10, Android 13, Android Studio Flamingo

Part 1: Make a Simple Interface

02. Creating Your First Composable Function

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Declarative UI's with Jetpack Compose Next episode: 03. Building a Simple Layout

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In order to build user interfaces with Jetpack Compose, you will need to use composable functions.

Using a Text Composable

In Jetpack Compose, you can add some text onto your UI using the Text composable function. Since it is an inbuilt composable, we only need to call it and provide any necessary arguments.

setContent {
    Text(text = "Hello Jetpack Compose")
}

Creating a Composable Function

Sometimes, you may be interested in building UI components that are not available by default with Jetpack Compose. For such scenarios, you can create your own composable function. These functions are usually annotated with the @composable property.

Composable Function

For now, let us create a composable function that will require only a simple text.

@Composable
fun TitleText(name: String){
    Text(text = name)
}
setContent{
    TitleText(name = "Hello Jetpack Compose")
}

Previewing Composables

Now, try playing around with your TitleText composable by changing the value of the name property.

Compose Preview

In order to set up the preview window for your composable, you need to add the @preview annotation to your composable.

@Preview
@Composable
fun AppPreview(){
    TitleText(name = "Jetpack Compose Preview")
}
@Preview
@Composable
fun TitleText(name: String = "Hello World"){
  Text(text = name)
}