Android Realm Database Tutorial

This is android realm database tutorial.

Realm is an open source database that can be used to store data locally in android. It is used by over 100k developers and is a good alternative of SQLite.

In this tutorial I will teach you how to use realm database in android.

Android Realm Database Tutorial

Android Realm Database Tutorial

Installation

Before using realm we have to add it to our project.

Go to project level build.gradle file and add following line under dependencies section.

Now go to app level build.gradle file and add following line at top.

Sync your project.

At the time of making this tutorial the current version of realm is 2.2.1. Read this to know how to add latest version to your project.

Creating Realm

When the app is launched we have to initialize realm. It should be done only once.

To get realm instance we have to use getDefaultInstance() method.

Models

Model classes are used for storing data in realm. A realm model class can be created by extending RealmObject class.

Suppose we want to store details of student then the model class can be created in following way.

In this way you can have several model classes in your project.

For making any variable primary key just add @PrimaryKey annotation before it.

Write

Any write, update or delete operation should be performed within beginTransaction() and commitTransaction() methods.

Let say we want to add a student data then it can be done in following way.

Read

All students data can be retrieved in following way. For accessing the records just iterate through RealmResults inside a loop.

Realm provides several other methods for sorting the result. Suppose you want to fetch student with roll number 20.

Update

For updating a record first fetch it and then change the value of object using setter methods. Suppose you want to change the name of student with roll number 20 then it can be done in following way.

Delete

Realm provides several methods for deleting a record.

Android Realm Database Example

Below I have shared one example that manages student information using realm database.

Create an android studio project with package name com.androidrealm.

Create a model class with following code.

Student.java

Create an activity and add following code.

activity_main.xml

MainActivity.java

Save and run the project.

Screenshot

Android Realm Database Example

To learn more about realm you can read realm android docs at https://realm.io/docs/java/latest/

Comment below if you have any queries or found any information incorrect in above android realm tutorial.

3 thoughts on “Android Realm Database Tutorial

  1. Bhupendra Mishra

    Hie Neeraj,Well I Need Your Guide in Android Development.Actually I ‘m Doing a Management Project in Android Studio.It will be more better if i can talk to you on phone .So that I can clearly describe my project to you.

    Reply
  2. Khurram

    hello..
    can you help me in guiding in how to read from local realm database in android.

    I have used that database in my ios app but now I have exported it and want to use it in the android app
    as well.

    kindly share your thoughts if you can help me in that ..

    Thanks in advance!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *