Android Realm Database Replacement for SQLite and ORMs

Android provide SQLite database for local database in the android application. SQLite is hard to setting up because it require a lot of implementation and sql command. Due to performance of SQLite, it is much slower than Realm and other ORMs database. So, I would like to suggest the best local database for you which is Realm database. Realm database is easy to setting up in just a few minutes instead of hours if you are a experts. It is fast in performance because it have its own C++ core and aims to provide a better database to mobile application. In this tutorial, I will teach you how to use android realm database in the mobile application to perform create, update , delete and retrieve features.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RealmExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add a Dependecy

Open build.gradle (Project : RealmExample) and add realm dependency from the below and put inside the dependencies section.

Add a plugin

After that, open build.gradle (Module :app)  and apply a plugin after the dependencies section.

Create a class to extends Application

Right click your package name and create a new class name to CustomApplication. This class is to add the realm configuration in your application.

Edit AndroidManifest.xml

Add name in the application section. I add android:name”.CustomApplication” in the application level as show below source code. It is to define the startup application class.

Create a new class

Besides, create a new class for perform the create, retrieve , update and delete feature for realm database to use it later. Therefore, copy and paste the java code in the below section.

Create a class model

Create a new model class and name it as “User” and extends the class to RealmObject as the following source code below.

Create a new class for Recyclerview Adapter

After that, add a new class and name this class to userAdapter, this adapter is to show each row of content. The following source code is the example on how to write the adapter.

Add two new Empty Activity for Add User, Edit User and Delete User.

Create two empty activity in your project. The first activity name is AddUserActivity while another activity name is UserDetailActivity is for Edit and delete the selected user.

Add a new xml layout and edit it

Create a new xml in the res>layout folder name it to user_list_row.xml then copy and paste the xml source code in the below. It the layout of recyclerview item.

Edit activity_add_user.xml layout

Open activity_add_user.xml and follow the source code in the following section. It is the layout of add user activity.

Edit activity_user_detail.xml layout

Open activity_user_detail.xml and modify to the following code.

Edit activity_main.xml layout

Edit your activity_main.xml to the source code that i provide in the bottom.

Edit AddUser.java class

This class is to perform add user in the realm database.

Edit UserDetailsActivity.java class

After that, edit your userdetailsactivity.java to perform delete and update existing user feature in the realm database.

Edit MainActivity.java class

Open your mainactivity.java class and copy and paste the following source code. This class is to show the user from the realm database.

Run your project

Finally, you done the create, update, retrieve and delete feature in the realm database. So you can run the project and try it in the android application.

(Android Realm Database Replacement for SQLite and ORMs)

Source Code

(Visited 449 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

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