Android Room Database Example – Building a Todo App

Hi everyone, in this post we will learn about another architectural component of android which is Room Persistence Library. In this android room database example, we will learn how to use room for handling our …

android room database example

Hi everyone, in this post we will learn about another architectural component of android which is Room Persistence Library. In this android room database example, we will learn how to use room for handling our SQLite database.

Here is a new Android Room Database Example using Kotlin.

This is a video tutorial series and throughout this we will build a basic notes application using Room persistence library. 

What is Room?

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

Basically, with the help of room we can quickly create sqlite databases and perform the operations like create, read, update and delete. Room makes everything very easy and quick.

Components of Room

We have 3 components of room.

  1. Entity: Instead of creating the SQLite table, we will create the Entity. Entity is nothing but a model class annotated with @Entity. The variables of this class is our columns, and the class is our table.
  2. Database: It is an abstract class where we define all our entities.
  3. DAO: Stands for Data Access Object. It is an interface that defines all the operations that we need to perform in our database.

I will not go to further theoretical details of Room. But you can check this link for more details.

Android Room Database Example

So what we are going to do is, we are going to create a To Do application using SQLite and in this application we will learn how we can use Room for handling SQLite.

Creating a new Project

  • So create a new project in Android Studio. I have named this project as My ToDo.
  • The first thing that we will do is we will add all the required dependencies.

Adding Dependencies

  • Come inside app level build.gradle file and add the following dependencies.

  • After adding the above dependencies sync your project and you are good to go.

Creating Activities

  • We already have a MainActivity created, other than this MainActivity we need, AddTaskActivity and UpdateTaskActivity.
  • So create 2 more activities and name them AddTaskActivity and UpdateTaskActivity.

MainActivity

  • Before designing the activities define these colors in your colors.xml.

  • Come inside activity_main.xml and add the following xml code.

  • In our main activity we will display all the added task, and an add button at the bottom from where user can add a new task.
  • For displaying all the added task we defined RecyclerView and FloatingActionButton for adding a new task.

android room database example

AddTaskActivity

  • From this activity we will add a new task to our application. So we need an interface like this.

android room database example 1

  • For designing the above screen write the following code.

UpdateTaskActivity

  • Finally go inside activity_update_task.xml and write the following xml code.

Tasks RecyclerView Layout

We will display all the tasks added in a RecyclerView, and for this we need one more layout file. So create a layout file and name it recyclerview_tasks.xml and write the following xml code inside.

Creating Entity

Now here come the main thing, for every table that we need we need to create an entity. And for this application we need a single entity.

  • So create a class named Task.java and write the following code.

  • As you can see in the above code we have annotated the class with @Entity this is our table, and for the column id we have used @PrimaryKey(autoGenerate = true) this means this id will be auto increment, for other columns we used @ColumnInfo(name = “columnname”)

Creating Dao

  • Now we need the Dao (Data Access Object). So create an interface named TaskDao.java and write the following code.

  • You can see above we defined all the methods needed for the Create, Read, Update and Delete operation.

Creating Database

  • Now create one more class and name it AppDatabase, and write the following code inside the class.

  • In the above class we define all the entities and the database version.

Database Client

  • Creating AppDatabase’s object is expensive so we will create a single instance of it.
  • Create a class named DatabaseClient and write the following code.

Adding a Task

Now lets perform the create operation, which is adding a task to the database.

  • Come inside AddTaskActivity and write the following code.

  • The above code is very simple, we created an AsyncTask to perform our operation because if we will try to perform the database operation in main thread it will crash our application.
  • For saving the task we just created the object and called the insert method that we created in our TaskDao interface.
  • You can try testing this but first you need to open this activity and for this you need to code the open functionality in the MainActivity. So what you have to do to test it is, attach a click listener on the add button that we have created in MainActivity and open AddTaskActivity when the add button is clicked.

Reading All Tasks

Now we will read the saved task from the database, and we will display it on the RecyclerView.

RecyclerView Adapter

  • Before going further, let’s first create our adapter. So create a class named TasksAdapter and write the following code. If you don’t know about RecyclerView then you can check this Android RecyclerView Tutorial.

  • In the above code we also attached a click listener to our itemView so that we can fire an event when a task is clicked. When a task is clicked we are opening the UpdateTaskActivity and we are also passing the selected task using intent.

Reading Tasks

  • Come inside MainActivity and write the following code.

  • The code is very simple, we just called the getAll() method that we created inside our TaskDao to get all the stored tasks from the database.
  • Then we are displaying the read tasks to a RecyclerView.

Updating and Deleting Task

Now finally the Update and Delete operation we will perform in the UpdateTaskActivity.

  • Open UpdateTaskActivity and write the following code.

  • In the above code, again we are doing the same thing, we are calling the methods we created inside TaskDao using an AsyncTask.
  • Now the app is complete you can try running it.

Todo App

  • So guys we have build the todo application successfully. From below link you can download the apk file.

Download APK

Android Room Database Example Source Code

If you are having any problem following this post then you can also get the complete source code from the below link.

So that is all for this Android Room Database Example friends. I will keep posting more tutorial about Android Architecture Components, meanwhile you can share this post with your friends. And for any question leave that on the comment section below. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x