Start a Project

Jetpack Compose Android

Jetpack Compose is a modern toolkit for building native Android UI. It’s based on the declarative programming model, so you can simply describe what your UI should look like, and Compose takes care of the rest—as app state changes, your UI automatically updates.

This feature is currently in alpha version but we really need to think about it this early.

Jetpack compose is currently present in Android Studio 4.0 and above which is in the canary build which means currently there might be some bugs in it and shouldn’t be used in the production version of the apps.

Some configuration changes needed to use Jetpack Compose

You need to set your app’s minimum API level to 21 or higher and enable Jetpack Compose in your app’s build.gradlefile, as shown below:

Jetpack Compose currently requires an experimental version of the Kotlin-Gradle plugin. To include this plugin in your app, include the following dependencies in your project’s build.gradle file:

Include Jetpack Compose toolkit dependencies in your app’s build.gradle file, as shown below:

A simple Compose App: Hello World

Let’s take a look at the code needed for a simple “Hello World” app with Jetpack Compose.

In the onCreate method we set the content of our Compose app by calling setContent. This is a method that initializes the composable widget tree and wraps it in a FrameLayout.

To make things work we need to wrap our app in a CraneWrapper and MaterialThemeCraneWrapper is responsible for setting up providers for the ContextFocusManager and TextInputService. The MaterialTheme is needed to provide colors, styles, and fonts to your widgets. With this in place, we can add a Textcomponent that will render our text on the screen in a certain style.

 

References :

Exit mobile version