Android RoomDatabase, LiveData and ViewModel

Room Database is an architectural component that allow fluent databases access in mobile applications.
Room Database provides abstraction layer over sqlite databases making easy to perform database operations which are Select, Update, Delete and Insert.

Major Components in Room Database:

  • This is the main access point for underlying connection to your app’s persistent relational data.
  • Usually annotated with @Database.
  • Abstract class that extends RoomDatabase.
  • Contains abstract class that has no arguments which returns class annotated with @Dao.
  • Contains a list of entities.


  • Represents tables in the database.


  • Contains methods used for access the database.

We shall create an application which allows users to insert new record and display the record using LiveData.

LiveData is an observable data holder class, it’s lifecycle aware meaning only updates app component observers that are only in an active lifecycle state.

Let’s begin by adding RoomDatabase Dependency in our gradle file.

Open your gradle file and add the following code:

Creating our database table ( Entity).

In our database we are going store name of wild animals and annotate it with@Entity

  • @primaryKey Serves as the primary key for our table.
  • @ColumnName Serves as the name for our column just like a normal database.

Create Data Access Object (DAO)

Create class AnimalDao and annotate it with @Dao.

These class contains all the methods needed to perform database operations.

This class contains all the database operations, for the LiveData which is a  LifeCycle library that helps to observe changes in the stored data so that we can take necessary action.

Create RoomDatabase which is an abstract of SQlite.

Create a AnimalRoomDatabase class, This class should extend RoomDatabase and is annotated with @Database.

Create Repository.

A Repository manages query threads, It provides a clean API to the rest of the app.

Contains member variables for the dao

Create AnimalRepository class.

Create a view model.

Takes care of holding and processing all data needed for UI.

We use LiveData in view model because of the following benefits.

  • Use of observers to only update UI when data actually changes instead of polling for changes
  • No database call from viewmodel.

ViewModel extends AndroidViewModel class

Create XML file for RecyclerView and it’s Items.

In your @layout/content_main.xmlreplace the TextView with recyclerview

Create another Layout xml file for recyclerview items.

Creating Adapter InOrder to show our data.

Create class AnimalAdapter that will populate our
recyclerview with data.

Adding recyclerview to our MainActivity.

Populating our database.

To populate out database we need to create another activity.

Create a new Activity File > New > Activity > Empty Activity .

Call your activity InsertActivity.

Insert activity_insert.xml Create an edit text and button.

User types on the edit text and when the button is clicked the data is inserted in our database.

In you InsertActivity class add the following code for inserting thee data into our database.

Finally in you MainActivity handle on onActivityResult where is the data is present we are going to insert it else we are not going to inser. Also in our main Activity we are going to listen for data change using observers.

That’s all, run your app the following output will be displayed.

To get the full source code, us the link below.

Thank you for any question or clarification. Kindly comment below.