JetPack Compose With MVVM

This article is about creating an app with Jetpack Compose. We are going to follow basic MVVM architecture and will use Retrofit library and . And for data, we will use the api’s

Screenshot of final app

Jetpack Compose is a modern toolkit designed to simplify UI development. It is fully declarative, meaning you define your UI by calling series of functions that transform data into a UI hierarchy. And one more important thing about compose is when the underlying data changes framework automatically recalls compose functions and updates the UI.

Before going into details I hope you know the basics about compose and know how to use compose(If no, then I highly recommend you to go through the basics). Attaching some relevant links related to compose


You will need the Android Studio Arctic version to work with jetpack compose. you can download the beta version from below link. Jetpack compose won’t work with default android studio which you may be using.

Start a new android studio project and select Empty Compose activity, give your app name and package name details.

Go to app build.gradle file and make sure compose is enabled

We have a data layer and UI layer for this project. Data layer will contain all data fetch from themoviedb and mapping to our model. Ui layer will have the composable functions. There are 100’s of articles related to data fetch using retrofit, so I’m not going to discuss that.For reference, I’m attaching the GitHub link for the complete source code.

Before going to the implementation add this library to build.gradle


So by this time, your data fetching part should be done. So next step is to create your ViewModel class. I named it as MainViewModel.

in ViewModel class, define API service , repository class.Next thing is to define your movie list and mark it as mutableStateOf(). which is basically an observable type integrated with compose-runtime which we added to our build.gradle.The next thing is create fetchTrendingMovies() function,inside that fetch your data from server and assign the data to the list which we defined above(trendingMovies).

Next step is to create different composable function for different components.

We need basically four composable function

  1. MainMovieList. — The list view
  2. ListviewItem
  3. MovieImageBanner
  4. MovieMetadataItem

So will create each item first then add it to the mainlist.

First, create a class called ListViewItem.kt


This is the two text views showing title and rating ,right to the imageview

Modifier can be used to set padding ,margin,width etc.

Column is used to align items vertically.

for compose functions you have to use @Composable annotation.

Our next component is Image view.for imageview we are going to use Coil(It’s an image loading library backed by google).for that add coil to build.gradle

Next is to create a composable function for image

rememberCoilPainter is provided by Coil library,you can pass option to make roundcorner or any image related attributes to rememberCoilPainter.

For more details about coil usage

Next is to have a parent function that will add these two

this listview item have Card and Row to align items horizontally.

Now we have to add this items to the main list based on number of items.

For that create class called ListViewMain.kt

In this Composable function,we are calling ListViewItem function for each and every item of list.itemsIndexed will iterate through the entire list, and we used rememberListState() to update the view during the state changes.

stickyHeader is added to give Title for the page.Which is a normal composable function with a title(It’s a api which can be deprecated in future,so add annotation to function @ExperimentalFoundationApi,otherwise it will give error)

So the data part and UI partis done. Now it’s time to connect between this two. For that create a composable function and pass mainViewModel object .

Final step is to call the above function from your mainActivity.

If you run the project now you will be able to see a listview with image and details.

Next step is to add a click event for the list item.We will cover that in Part Two.

Part two can be found from below link

You can find the full source code from github

Happy Reading… Happy Coding..

Passionate android developer working with android mobile and androidTv