What is Jetpack Compose?

Jetpack Compose is a modern, fully declarative UI toolkit introduced by Google in mid-July 2021, to simplify the process of building native interfaces in Android. It’s part of the Android Jetpack, which is a suite of libraries designed to accelerate Android development.

Jetpack Compose represents a significant step forward in Android UI development. It is built with Kotlin and uses a reactive programming model, which means we can describe our UI as functions of our application state.

Understanding Jetpack Compose

In Jetpack Compose, our UI is made of Composables. A Composable is a reusable piece of UI, defined as a Kotlin function annotated with @Composable. Each composable function represents a part of our UI and can be as simple or complex as needed.

@Composable
fun Greeting(name: String = "Android") {
    Text(
        style = MaterialTheme.typography.headlineLarge,
        text = "Hello $name!"
    )
}

Composables are reactive by nature, which means that when the state of our data changes, the UI automatically updates to reflect those changes.

@Composable
fun ColorChangingScreen() {
    var color by remember { mutableStateOf(Color.Red) }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(color)
    ) {
        Button(onClick = {
            color = Color(
                red = Random.nextFloat(),
                green = Random.nextFloat(),
                blue = Random.nextFloat()
            )
        }, modifier = Modifier.align(Alignment.Center)) {
            Text(text = "Change Color")
        }
    }
}

Composables can nest within other Composables, forming a hierarchical structure akin to a view tree in traditional Android development. This flexibility allows us to create complex, dynamic UIs with ease.

@Composable
fun ComposeExample() {
    ColorChangingScreen()
    Greeting("Android")
}

Why the Buzz Around Jetpack Compose?

Jetpack Compose simplifies and speeds up UI development for Android with less code, powerful tools, and intuitive Kotlin APIs. Traditional Android UI development relies on using XML for layout designs, which can be verbose and not very intuitive. However, with Jetpack Compose, we can build UIs programmatically using Kotlin, resulting in more maintainable and readable code.

Imagine we want to create the next piece of UI

Here is how we would do it using traditional XML

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        app:cardBackgroundColor="@color/bg_color"
        app:cardCornerRadius="25sp">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="16dp">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="300dp"
                android:contentDescription="@string/banner"
                android:src="@drawable/bannerwwco" />

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/logo"
                android:src="@drawable/wwcologo" />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/message"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:padding="16dp"/>

    </LinearLayout>
</androidx.cardview.widget.CardView>

And this is how we would do it using Jetpack Compose

@Composable
fun ComposeCardExample(name: String = "Android") {
    Card(
        colors = CardDefaults.cardColors(containerColor = colorResource(id = R.color.bg_color))
    ) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(painter = painterResource(id = R.drawable.bannerwwco), contentDescription = "banner")
            Image(painter = painterResource(id = R.drawable.wwcologo), contentDescription = "logo")
            Text(
                text = "Hello $name! Welcome to the Wawandco blog",
                modifier = Modifier.padding(16.dp),
                color = colorResource(id = R.color.white),
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }
}

Now, you may ask, why would we want to use Jetpack Compose?

Simplicity and Conciseness

The Jetpack Compose code is more concise and easier to read. In the traditional XML approach, we have to specify attributes like android:layout_width and android:layout_height for every view, even if they are the same for most of our views. In Jetpack Compose, these are often unnecessary as the layout behavior is defined by composables and modifiers.

Kotlin-Based

Jetpack Compose is written entirely in Kotlin, which means we can take full advantage of Kotlin’s features such as null safety, coroutines, and more.

Reactive UI

In Jetpack Compose our UI automatically updates when our app’s state changes. In traditional Android development, we would have to manually update our views when our app’s state changes.

Modularity & Reusability

In Jetpack Compose, UI components are functions that can be reused to build more complex UIs. This can lead to more modular and maintainable code.

Preview Feature

Jetpack Compose provides a preview feature that allows us to see our Composables without needing to run our app.

Key Features of Jetpack Compose

Declarative UI: Unlike the traditional imperative programming model, where we need to describe the steps to achieve a result, Jetpack Compose uses a declarative approach. we declare what our UI should look like for a given application state, and the system takes care of updating the UI when the state changes. This approach simplifies UI development and makes our code easier to understand.

State Management: Jetpack Compose provides a robust state management mechanism. It ensures our UI always reflects the current state of our app, making it easier to create dynamic and interactive UIs.

Interoperability: Jetpack Compose is designed to work seamlessly with existing Android views. This means we can adopt Jetpack Compose incrementally in our existing Android projects without having to rewrite our entire app.

Material Design Out of the Box: Jetpack Compose comes with built-in Material Design components. This allows us to create beautiful, standardized UIs without having to manually implement the Material Design guidelines.

Closing Thoughts

Jetpack Compose is a new way of building UIs on Android that is set to revolutionize the development process. It simplifies UI development, making it more efficient and intuitive, so we can focus more on creating great user experiences.

Not only does it reduce the amount of boilerplate code, but it also makes the code more readable, making application maintenance and iteration easier. This leads to higher-quality apps and faster development cycles, which benefits both developers and users.

Jetpack Compose is already being adopted by major companies like Google, Reddit, Dropbox, X, Threads, and many others. This demonstrates its practical benefits and industry acceptance. At Wawandco we have also embraced Jetpack Compose into our stack of technologies, which has allowed us to deliver high-quality user experiences more efficiently.

As if that wasn’t enough, Jetpack Compose is also the foundation of Compose Multiplatform a declarative framework for sharing UIs across multiple platforms. But we’ll have a different post to go over this, for now, We are excited about the future of Android development with Jetpack Compose and look forward to seeing its impact grow as time goes on.

References