An Introduction To Jetpack Compose

Jetpack Compose

Jetpack Compose is a modern Android UI toolkit that makes it faster and easier to build user interfaces for native Android apps. Before Google introduced Jetpack Compose Android user interfaces were written in XML which has too much boilerplate, logic specific to a UI element lives in a separate Java or Kotlin file, this makes the APK file size large and may cause maintenance to be a nightmare as the app gets bigger.

With Jetpack Compose the UI is written in Kotlin and the logic specific to the UI can be in the same file as the UI, this promotes separation of concerns and makes it easier to test the app, maintain, and most importantly; the APK size becomes smaller.

The Jetpack Compose framework has prebuilt UI components that can be used to construct complex UI. These components are called Composables, they are similar to widgets in Flutter. Some of Jetpacks Composables are: Text( ), Row( ), Column( ). ivermectin 12 mg tablet uses in hindi wikipedia

We’ll use these Composables to build our own custom Composable that will look like a tweet.

Before building the Tweet Composable, we need to understand how these Composables are laid out. In Compose, the Row and Column Composables are used to build a layout, Column lays out Composables from top to bottom and Row lays out Composables from left to right. ivermectin alcohol interaction Every Composable can have a Modifier parameter which allows us to change how a Composable is presented.

The image below shows how the Tweet Composable is laid out.

To follow this tutorial you’ll need the latest version of Android Studio. Create a new project, select the empty compose activity template and add the following dependency to the app level gradle file:
implementation “”

Coil is an image loading library, we’ll use it to load remote images such as the profile picture and the tweet attachment. Because we’ll be loading images from the internet, make sure that the Internet Permission is turned on.

Preparing the data

We need a data class that represents a tweet, this will be used by the Tweet Composable to render the data. We’ll create Tweet and User data classes because each Tweet has to be associated with a User. These data classes will live under the model package.

We’ll mock our tweets for this tutorial, so create a data package and add an object that will hold a list of fake tweets, add as many tweets as you will.

Building the Tweet Composable

We’ll first build the layout, then we’ll add Composables bit by bit until everything has been added.

To create a Composable, you need to write a function and annotate it with @Composable, this tells Jetpack Compose that the function defines UI.

The Tweet will be wrapped with a Row Composable that is modified to take up the entire width of the screen.

Inside the Row, we’ve got two Column Composables, one for the profile picture and one for the tweet. Because these Columns are inside a Row, they will be rendered from left to right.

Adding the Profile Picture

Inside of the profile pic Column, add the Image Composable and use rememberCoilPainter from Coil as the painter. Using the Modifier, change the size of the image to 55 dp and then clip it to a circular shape.

Adding the Tweet details

Use the Text Composable to display the name, username, and date. To add some space between the texts use the Spacer Composable. Text( ) takes the style parameter, use this for styling. So far we’ve added the profile picture and the tweet details.

Adding the Tweet

Now to add the actual tweet, create another Column below the tweet details Row. Inside of this Column add the Text Composable that will display the tweet, then check if the tweet has an image, if it does, use the Image Composable with Coil to display that image.

Adding the Bottom Icons

To create the bottom buttons we’ll use the IconButton and Icon Composebles. These will be in a Row that adds space between Composables.

Now our Tweet Composable is done! To display it we’ll need to create another Composable called TweetList, it will take a list of tweets and render a Tweet Composable for each Tweet. To display the Tweets we’ll use the LazyColumn Composable which behaves like a RecyclerView. praziquantel albendazole triclabendazole, bithionol,nitazoxanide substitute with ivermectin

Displaying Tweets

Inside of the setContent block under MainActivity we’ll get a list of our fake tweets and the call TweetList passing the list of tweets.

And that’s how easy it is to build native UI with Jetpack Compose, no more fragments and activity files, and no more RecyclerView boilerplate code.

About the Author