Update note: Joey deVilla updated this tutorial for Android Studio Giraffe, Kotlin 1.9 and Android 14. Alex Sullivan wrote the original.
Weâre at an exciting point in Android development. According to a survey of the mobile development ecosystem taken in late 2022 by the Mobile Native Foundation, half of Android developers are building apps with Jetpack Compose. The other half are building them âthe old way.â
Operating systems evolve, and Android â the worldâs most popular OS â is no exception. When a platform the size of Android makes a change this big, the first developers who embrace the change gain a significant advantage. With half the Android developers still waiting to make the leap, the time to learn Jetpack Compose is now.
What is Jetpack Compose?
Released in July 2021, Jetpack Compose is a UI toolkit that updates the process of building Android apps. Instead of XML, you use Kotlin code to declaratively specify how the UI should look and behave in various states. You donât have to worry how the UI moves among those states â Jetpack Compose takes care of that. Youâll find it familiar if youâre acquainted with declarative web frameworks such as React, Angular or Vue.
The Jetpack Compose approach is a significant departure from Androidâs original XML UI toolkit, now called Views. Views was modeled after old desktop UI frameworks and dates to Androidâs beginning. In Views, you use a mechanism such as findViewById() or view binding to connect UI elements to code. This imperative approach is simple but requires defining how the program moves among states and how the UI should look and behave in those states.
Jetpack Compose is built with Kotlin, and it takes advantage of the features and design philosophy of Kotlin language. Itâs designed for use in applications written in Kotlin. With Jetpack Compose, you no longer have to context-switch to XML when designing your appâs UI; you do everything in Kotlin.
In this tutorial, youâll build two Jetpack Compose apps:
A simple test run app, which youâll build from scratch, starting with File â New.
A more complex cookbook app that will display a list of recipe cards containing images and text. Youâll build this using a starter project.
Your First Jetpack Compose App
Ensure youâre running the latest stable version of Android Studio. Both apps in this tutorial â the simple app youâre about to build and the cookbook app youâll build afterward â were built using the Flamingo version of Android Studio. Lately, Google has been upgrading Android Studio at a furious pace, and the code below might not work on earlier versions.
Note: âCheck for Updatesâ is your friend! On the macOS version of Android Studio, youâll find it under the Android Studio menu. If youâre a Windows- or Linux-based Android Studio user, youâll find it under the Help menu.
Once youâve confirmed your Android Studio is up to date, launch it and select File â New â New Projectâ¦. Depending on how you last resized the New Project window, youâll either see something like this:
or this:
Either way, youâll see the first template in the list is for an Empty Activity project with the Jetpack Compose icon:
In the world of programming, where you have to state matters explicitly so a compiler can understand them, this is considered a subtle hint. You should infer that Jetpack Compose is expected to be the preferred way for building Android UIs going forward, and the sooner you learn it, the better.
Select the Jetpack Compose Empty Activity template and click Next. In the following New Project window, name the project My First Compose App and click the Finish button.
Hello, Android!
Once Android Studio finished building the project, run the app. You should see something like this:
To see whatâs behind this particularly unexciting screen, open MainActivity.kt. It still contains a MainActivity class and an onCreate() method, and onCreate() still calls on its counterpart in MainActivityâs superclass, ComponentActivity.
Whatâs different is the rest of the code in onCreate(). When building Android UIs the old way â which is called Views â onCreate() calls the setContentView() method and passes it the ID of the viewâs XML file, which Android uses to render the onscreen elements. In Jetpack Compose, onCreate() calls a method named setContent(), and in the default project, it looks like this:
setContent {
MyFirstComposeAppTheme {
// A surface container using the \’background\’ color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting(\”Android\”)
}
}
}
setContent() takes a lambda as its parameter, and near the end of that lambda is a call to a method called Greeting(). Youâll find its definition immediately after the MainActivity class:
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = \”Hello $name!\”,
modifier = modifier
)
}
As you see, Greeting() is the method that determines what appears onscreen when you run the app. You should also notice the following elements of this method:
Itâs annotated with @Composable. This informs the compiler that Greeting() is a composable function (or composable for short), which means it receives data and generates a UI element in response. One reason to make it clear that a function is composable is that composable functions can only be called by other composable functions.
Source link