Android Jetpack Compose — Exploring State Based UI

As a developer who is always excited about new enhancements in Technology, I recently started exploring Jetpack Compose, Android’s own Modern UI toolkit to build Native UI in declarative style inspired from Modern frameworks like Flutter, litho etc.
I have been playing with it for a while now and it is super fun, simple and exciting to build user interfaces and state based animations using it, hence thought of spreading the knowledge by sharing what I have learnt using this amazing tool so far.
Table of contents
- Why JetPack Compose
- Building UI using Compose
- Handling UI state
- Interacting with ViewModels
- Compose LazyGridFor with paging
Why Jetpack Compose ?
Like with every new feature, there will always be few mandatory questions which needs to be addressed before the transition, like for starters learning curve of the feature, interoperability with current system and ease of migrating existing code. As per my experience playing with compose for past couple of weeks, it squashes above questions right away
- Jetpack Compose comes with built-in support for Material Design, Animations, Dark Theme and most of the UI Compose widgets you use in Android.
- It is interoperable with current Android view system, you can switch to Compose world from view system using
ComposeView
and from compose to traditional Android views usingAndroidView
compose function. - With interoperability in place, migration is piece of cake and can be done step by step, you can start creating new screens using compose by keeping existing code intact and slowly moving existing screens when and ever you feel comfortable.
- Declarative, Concise, State based data flow and most exciting part is writing UI in our beloved language Kotlin ❤
Thrilled ? Me too! So without further ado let’s go ahead and build a sample Android Application using Jetpack Compose. Most of the apps includes loading data into lists/grids using a loading and error state, So this article demonstrates most commonly used scenario which is to display a list of items by fetching data from server along with loading and error screens, also paginating them.
To start working with Jetpack Compose, Install Android Studio 4.2 latest canary and create new project with Empty Compose Activity template to let IDE set up compose dependencies and Gradle configurations for you. For more information visit https://developer.android.com/jetpack/compose/setup
Building UI using Compose
Compose widgets are created using annotating functions with @Composable
We need to first build compose functions for states of screen which are LOADING
, ERROR
, EMPTY
and DATA
, also IDLE
if needed.
In this case we need three compose widgets to show Loading, Error and Empty UI states. Also let’s make sure error and empty messages are configurable so we can reuse same functions across Application.
Handling UI State
Now the Compose widgets are ready, time to render widgets based on current state of screen. To do that let’s create enum class with all possible states.
State
is a value holder which holds a value and the compose which is using State
to read the value will be subscribed automatically to the changes of value inside that state
, meaning when ever the value changes , the current RecomposeScope
will be redrawn using new value.
Create a compose function using State
value as PageState
to draw/redraw compose widgets based on current state of the page
Interacting with ViewModels
So far so good, State handler Compose widget is ready, but it needs State<PageState>
to dynamically update UI, to achieve that let’s create a ViewModel
which is responsible to update value of State<PageState>
based on current mode and also to fetches data from server alongside providing scope for pagination.
Also to implement paging logic in ViewModel
, create a class which appends the new result to existing one
Now to wire compose widgets with ViewModel
, Fragment
is used as a UI container, and by using ComposeView
inside a Fragment
, it supports compose widgets
Perfect! App is almost ready, at this point it can render loading, error and empty UI on Fragment
, let’s look at how to create GridView
in compose with paging
to fill the content part of the screen once loaded.
Compose LazyGridFor with paging
Out of the box, Jetpack Compose toolkit provides RecyclerView
equivalentLazyRowFor
and LazyColumnFor
to stack items vertically and horizontally with lazy implementation. Currently, there is no LazyGrid
support provided in-built.
Create one by merging LazyColumnFor
and Row
along with few modifications to data. Also make sure proper index of item
is calculated as it will be used for paging purposes.
Create a grid item to populate LazyGridFor
and also a model class to bind data into grid item.
Now Let’s fill Fragment
content with data using LazyGridFor
and implement paging logic to fetch data page by page.
🎊 That’s it ! We finished the entire flow of displaying data into grid with loading states in between. Now let’s look at the code in Action! 👏 👏



The complete implementation of app Plasma is in below repository 👇, It comes with cool Jetpack Compose widgets like Gradient ProgressBar , Button ,Text, State based transitions and much more. Also built using Modern Android Development(M.A.D) tools like flows, coroutines , Navigation, Architecture components, Jetpack Datastore and Dagger Hilt.
Thanks for reading! Please click the 👏 button and share it to help others only if you feel it is worthy!
Please drop any suggestions, enhancements, thoughts or queries in comments.