DEV Community

Cover image for Android widgets – Update using Kotlin Flow, Room and Dagger-Hilt
Inspire Coding
Inspire Coding

Posted on

Android widgets – Update using Kotlin Flow, Room and Dagger-Hilt


In this Android tutorial I'm going to show you an approach which helps to update the app's widget realy easy using Kotlin Flow, Room and Dagger-Hilt.


First of all, we have to talked in a few words about what does Koltin Flow, Room and Dagger-Hilt mean.

Kotlin Flow

Kotlin Flow is developed by JetBrains, the owner of the Kotlin language and it is a new stream processing API. It implements the Reactive Stream specification, and its goal is ot provide a standard for asynchronous stream processing. Kotlin Flow is build on top of Kotlin Coroutines.
Using Flow we handle streams of values, transform the data in a complex threaded way with only few lines of code.


The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.

The library helps you create a cache of your app's data on a device that's running your app. This cache, which serves as your app's single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.
Source: Room Persistence Library


Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
Hilt provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically. Hilt is built on top of the popular DI library Dagger to benefit from the compile-time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.
Source: Dependency injection with Hilt

The sample app

Sample app

So, after a short indtroduction, let's start coding. :)

Step 1 - Get the starter project

For this tutorial we are going to use a starter project, which is a ToDo app. This app contains Room and RecyclerView to show the created ToDo items.

The tutorial for the starter project is available on Inspire Coding: Room basics – Introduction


If you don't want to do the starter tutorial, then you can get the starter project from GitHub as well: GitHub

Clone or just download the project, and import it in Android Studio.

Step 2 - Add the widget

The next step is to add the "widget" package to the main source set.
If the package is created, then create into it a new widget with the below detailes

  • Width: 4 cells
  • Height: 1 cell
  • No Configuration Screen needed
  • Language: Kotlin

Step 3 - Widget's layout

The layout gonna be very simple.

  • TextView for the title
  • TextView for the due date
  • TextView for the description
  • ImageView for the priority
< RelativeLayout

        android:text="Task 1"
        android:textSize="14sp" />

        android:textSize="8sp" />

        android:textSize="10sp" />

        android:src="@drawable/prio_green" />

</ RelativeLayout>

The shape of the widget

Now create a new Drawable resource file with the name: shape_roundedcorners_white.xml and paste into it the below code.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="">



This will be the background of the widget.

Step 4 - Implement Hilt


Add the @HiltAndroidApp annotation to the MyApp::class


Then create a Hilt module into a new package called "di". This module will get the name of RoomDatabaseModule.
And the file looks like below.

class RoomDatabaseModule
    fun providesDatabase (application: Application) = ToDoRoomDatabase.getDatabase(application)

    fun providesCurrentWeatherDao (database: ToDoRoomDatabase) = database.toDoDao()


Thenafter add the @AndroidEntryPoint annotation to the AppWidget::class.

@Inject DAO

Finally modify the ToDoRepository::class to use a constructor injection in the header of the class for the ToDoDAO.

class ToDoRepository @Inject constructor (private val toDoDao: ToDoDao)

Step 5 – Add Flow to DAO

In this step we are going to implement the needed methods to get the todo item from Room which has the todoId of 1.
For this we are going to add the below method to the ToDoDAO::class.

@Query("SELECT * FROM ToDo WHERE toDoId = 1")
fun getFirstToDoItem() : Flow<ToDo>

Note that the method return Flow which wraps a ToDo type.

Step 6 – Extend the repository

Then add the below method to the ToDoRepository::class.

val getFirstToDoItem : Flow<ToDo> = toDoDao.getFirstToDoItem()

The return value is the same like the same method in the ToDoDAO::class.

Step 7 – Update the AppWidget::class

Thenafter we will add 3 member variables to the AppWidget::class.

private val job = SupervisorJob()
val coroutineScope = CoroutineScope(Dispatchers.IO + job)
@Inject lateinit var toDoRepository: ToDoRepository

Because Flow is a suspend function, we can call it only from CoroutineScope. That's why we need the Job and the CoroutineScope.
To get the item from Room is an IO operation, so we are going to add the Dispatchers.IO to the CoroutineScope.

Next, remove the onUpdate() and the onEnabled() methods. We don't need them.

Thenafter add the onReceive() method to the AppWidget::class.

override fun onReceive(context: Context, intent: Intent?)
    super.onReceive(context, intent)

    coroutineScope.launch {
        toDoRepository.getFirstToDoItem.collect { _toDo ->
            val appWidgetManager = AppWidgetManager.getInstance(context)
            val man = AppWidgetManager.getInstance(context)
            val ids = man.getAppWidgetIds(ComponentName(context,

            if (_toDo != null)
                for (appWidgetId in ids)
                        context, appWidgetManager, appWidgetId,
                        _toDo.title, _toDo.dueDate, _toDo.description, _toDo.priority

Now you should have an error, because we haven't extended the updateAppWidget() method. So, replace it with the below one.

internal fun updateAppWidget(
    context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int,
    title: String?, dueDate: String?, description: String?, priority: String?)
    // Construct the RemoteViews object
    val views = RemoteViews(context.packageName, R.layout.app_widget)

    if (title != null) {
        views.setTextViewText(, title)
    } else {
        views.setTextViewText(, "")
    if (dueDate != null) {
        views.setTextViewText(, dueDate)
    } else {
        views.setTextViewText(, "")
    if (description != null) {
        views.setTextViewText(, description)
    } else {
        views.setTextViewText(, "")
    if (description != null) {
        when (priority)
   -> views.setImageViewResource(, R.drawable.prio_green)
   -> views.setImageViewResource(, R.drawable.prio_orange)
   -> views.setImageViewResource(, R.drawable.prio_red)


    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views)

And one more error. It is there, because we haven't implemented the getPendingIntentActivity() method yet. This method will create for the views a PendingIntent to update the views if the widget's instances.
So, paste the below method at the end of the AppWidget.kt file.

private fun getPendingIntentActivity(context: Context): PendingIntent
    // Construct an Intent which is pointing this class.
    val intent = Intent(context,
    // And this time we are sending a broadcast with getBroadcast
    return PendingIntent.getActivity(context, 0, intent, 0)

So, we are almost done. One more thing left before we can test the app. When we delete an instance of the AppWidget, we have to cancel its Job as well. So add the below line to the onDisabled() method.


Run the app

Finally its time to run the app.

More Android tutorials

If you would like to do more Android tutorials like this, then visit my website:
Inspire Coding


I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

Top comments (1)

nurlibay profile image

Field inject not working
@Inject lateinit var toDoRepository: ToDoRepository