Android Room - Todo List Application With Examples

In this tutorial, we’ll be discussing and implementing the Room library introduced by Google. We’ll be using it to develop a Todo List Android Application.

Android Room

Room is a library introduced by Google that acts as an Abstraction layer over SQLite and makes the database querying easier.

Why use Room?It does compile-time verification of SQL queries. Thus preventing runtime crashes.

It reduces the boilerplate code. Instead of using SQLiteHelper and writing lengthy queries, we can use annotations to do the work for us.

Following is the structure of Room library:



Google Docs

Typically, Room consists of the following three main components:

  • Entity: This is a model class in which we define the properties which act as column fields. We can alternatively set column names using the annotation @ColumnInfo We need to set the table name too. To ignore a property from the table, use the annotation @Ignore over it. At least one property must have @PrimaryKey. To set a column name that’s different from the field name, use @Embedded
  • Database : This is an abstract class which must extend the RoomDatabase. We must set the Entity here. Also, we need to update the version number everytime we change the schema. Set exportSchema to true/false
  • Dao: Data Access Object. This is the interface where we set our SQL queries. @Insert, @Query, @Update @Delete.
    @Insert cannot return an int.
    @Update and @Delete can return an int which represents the number of rows changed/deleted.

Room queries can’t be and should not be executed on the main thread. It’ll lead to a crash.

Let’s use Room library in our Todo List Android Application.

Android TODO App Project Structure


Our application consists of inserting, updating, deleting todos.
We first need to import the following Room dependency in our build.gradle:

Let’s create the Table for our Database using the class as shown below:

This is an abstract class which contains the definition of the DaoAccess() interface.

Add the annotations and a few sql queries. And all the SQLite features are ready to be implemented in our Activity.

The code for the activity_main.xml layout is given below:

The code for the recyclerview_item_layout.xml is given below:

The code for the class is given below:

  • myDatabase = Room.databaseBuilder(getApplicationContext(), MyDatabase.class, MyDatabase.DB_NAME).fallbackToDestructiveMigration().build(); is used to initialise the database.
  • fallbackToDestructiveMigration() provides seamless migration across database version without crashing.
  • We check if the app is launched for the first time or not using shared preferences.
  • Then we create a dummy ArrayList to populate our RecyclerView.
  • We need to use asynchronous tasks to run queries. So either use AsyncTask or go for RxJava. In this tutorial, we’ve used AsyncTasks.
  • We will have following operations in our app:
    • Updating a Todo Item from the list
    • Adding a new Todo Item to the list.
  • For either of the cases, we use startActivityForResult to get back the data from the next activity and update the RecyclerView accordingly.
  • Our spinner is used to filter the RecyclerView based on category for which we Query the database.

The code for the activity_todo_note.xml layout is given below:


The code for the class is given below

setResult(RESULT_OK, intent); is used to pass back the result to the MainActivity.
We either pass the new todo task’s id or the number of rows deleted/updated and the RecyclerView is updated accordingly.

The output of the above application in action is given below.

This brings an end to this tutorial on Android Room. Using it we were able to create our first TodoList Application easily and quickly while reducing the boilerplate code of SQLite. You can download the final project from the link given below.

By admin

Leave a Reply

%d bloggers like this: