VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-build-a-simple-note-android-app-using-mvvm-and-room-database/

⇱ How to Build a Simple Note Android App using MVVM and Room Database? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Build a Simple Note Android App using MVVM and Room Database?

Last Updated : 23 Jul, 2025

Android provides us a feature with which we can store users' data inside their mobile itself with different storage options such as Shared Preferences, SQLite database, and the Room Database. All the data storing techniques are having different use cases. In this article, we will specifically take a look at using a Room Database with Architecture Components in Android. In this article, we will be specifically looking at components such as LiveData, ViewModel, and Room. We can get to see the explanation of each component in detail. Below is the guide for it. 

👁 Image

The components which we will be using inside the application are listed below with a detailed explanation : 

  • LiveData: Live Data is a data holder class that can be observed. It holds as well as caches the latest version of the data and notifies our observer whenever the data is being updated or changed. Live Data automatically manages all of this since it is aware of the relevant lifecycle status changes while observing.
  • ViewModel: View Modal acts as a communication center between repository and UI. The UI no longer needs to worry about the origin of the data. The ViewModel instances survive Activity/Fragment recreation.
  • Repository: Repository is a class that is mainly used to manage multiple sources of data.
  • Entity: Entity is an annotated class that is used to describe a database table when we are working with Room.
  • Room Database: Room Database is an improvised version of SQLite Database. It simplifies the database work and serves as an access point to the underlying SQLite database. The room uses DAO to issue queries to the SQLite database.
  • DAO: DAO is a Data Access Object which is used for mapping SQL queries to functions.

What we are going to build in this article? 

We will be building a simple Notes Application in which we will be displaying the list of notes in Recycler View which is added by the user. Along with that, we will be also able to add a new note to your app. We will be using Room Database for storing data in users' devices. Below is the video in which we will get to see what we are going to build in this article. Note that we are going to implement this project using the Kotlin language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Note that select Kotlin as the programming language.

Step 2: Updating your build.gradle.kts (Module :app) file

Navigate to the Gradle Scripts > build.gradle.kts (Module :app) and inside that, in the first plugins section, we have to add id for one more plugin. Below is the code for all the plugins which are present inside our build.gradle.kts.

plugins {
 ...
 id("kotlin-kapt")
}

Now add the below dependencies inside your build.gradle.kts file. We have to simply update the dependencies section with the below one. 

dependencies {
 ...
 // Room components
 implementation ("androidx.room:room-ktx:2.7.0")
 kapt ("androidx.room:room-compiler:2.7.0")

 // Lifecycle components
 implementation ("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7")
 implementation ("androidx.lifecycle:lifecycle-livedata-ktx:2.8.7")
}

After updating your gradle file. Now we have to sync your project and then we have to install all the dependencies inside the application by clicking on the sync now option at the top right corner.

Step 3: Adding new colors in the colors.xml file

Navigate to the app > res > values > colors.xml and add below colors to it. 

colors.xml:


Step 4: Creating an Entity

An Entity is basically a modal class or a structure of our database in which we will be used for creating a database structure. Inside our app, we will be having a simple table that will be only having two columns such as ID and a text. ID will be used for the identification of an entry and text is the column for the identification of text column. Below is the table structure for our database. Below is the image for the database. 

👁 Image

For creating a new Entity, we have to create a new Kotlin class. For creating this we simply have to navigate to your app's package name. Right-click on it then new and select Kotlin File/Class and then specify the Class name as Note and add the below code to it. Comments are added in the code to get to know in more detail. 

Note.kt:


Step 5: Creating a DAO class

DAO is a data access object which is used to specify SQL queries and then associate them with different method calls. DAO may be an abstract class or an interface. Inside the DAO class, we have to create different methods such as inserting, deleting the data, and reading the data from our database. So this class will basically interact with our database to add or delete data inside our database.


For creating a DAO interface we simply have to navigate to the app's package name, Right-click on it > New Kotlin File and we have to select as Interface and name it as NotesDao and we have to create this file. After creating this file we have to add the below code to it. 

NotesDao.kt:


Step 6: Adding a Room Database for our application

What is basically a Room Database?

  • Room is basically a database layer on top of the SQLite database.
  • Room takes care of mundane tasks that you used to handle with an SQLite Open Helper.
  • Room uses the DAO to issue queries to its database.
  • Room provides compile-time checks of SQLite statements.

Now to store data inside the user's device we have to create a Room database for storing the data. So for creating a database, we have to create an abstract class for creating our database. In this, we will be simply building our database using Room and we will be specifying our database name. For creating a new abstract class we have to go to the app's package name > Right-click on it > New > Kotlin file and we name it as NoteDatabase. After creating this class we have to add the below code to it. Comments are added in the code to get to know in more detail. 

NoteDatabase.kt:


Step 7: Creating a Repository Class

Repository class basically abstracts the access to multiple data sources such as getting the data from API or getting the data from Room database. A repository class will provides us a clean API for data access for the rest of the application. The repository will contain a logic that will be deciding whether we have to fetch the data from the network or we have to get the data from Database. 

Creating a Repository Class. For creating a Repository we have to create a new Kotlin file by simply right-clicking on your app's package name > Right-click on it > New > Kotlin class and specify the class name as NoteRepository and add below code to it. Comments are added in the code to get to know in more detail. 

NoteRepository.kt:


Step 8: Creating a ViewModel 

ViewModel is basically used for providing the data to our User Interface. It acts as a communication layer between Repository and the UI. We can use View Modal to share data between our fragments. For creating a View Modal we have to simply create a new Kotlin class and then we have to name it as NoteViewModel and add the below code to it. Comments are added in the code to get to know in more detail. 

NoteViewModel.kt:


Step 9: Working with the activity_main.xml file 

Now we will be working on the UI part of our application. Now we have to navigate to app > res > activity_main.xml and add the below code to it. Comments are added in the code to get to know in more detail.


Step 10: Creating a new layout file for our item of RecyclerView 

Navigate to the app > res > layout > Right-click on it > New > Layout Resource file and name your file as note_rv_item and add the below code to it. Comments are added in the code to get to know in more detail. 


Step 11: Creating an Adapter class

Now we will be creating an adapter class that will be used for setting the data to each item of our recycler view. For creating an Adapter class, we have to navigate to the app > java > your app's package name > Right-click on it > New > Kotlin/Class and name it as NoteRVAdapter and add the below code to it. Comments are added in the code to get to know in more detail. 

NoteRVAdapter.kt:


Step 12: Creating a new Activity for adding as well as Editing a note 

Navigate to the app > java > your app's package name > Right-click on it > New > select Empty Activity and name it as AddEditNoteActivity and create a new activity. After that navigate to the app > res > layout > activity_add_edit_note.xml file and add the below code to it. Comments are added in the code to get to know in more detail.


Step 13: Working with MainActivity.kt file

Navigate to the app > java > your app's package name > MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in more detail. 

MainActivity.kt:


Step 14: Working with the AddEditNoteActivity.kt file

Navigate to the app > java > your app's package name > AddEditNoteActivity.kt and add the below code to it. Comments are added in the code to get to know in more detail. 

AddEditNoteActivity.kt:

Refer to the following github repo to get the entire code:Simple-Note-Android-App-using-MVVM-and-Room-Database


Output:


Comment

Explore