All CategoriesAndroid App Development

Case Study On How to Use Realm Database in Android

We all know about local database using SQLite which is using for Internal Memory Storage in Android App Development and It is used for storing local data like Contacts, Phone details etc. Now I found one database which is lighter than SQLite – which is known as Realm Database, you can also say it as replacement of SQLite for local Database in Android.
You can download the latest version of Realm Database from Below Link:
Useful Link: https://realm.io/docs/java/latest/

  1. SimpleData will be stored in the database in the form of Object, so it will be easy to retrieve the data from the database.
  2. FastRealm Database is faster than SQLite database for query execution and maintenance of the database.
  3. Modern DevelopmentRealm database supports modern java features like generics, Vectors and Swift technologies.

Real Time Example
If you want to store data for offline mode usage like how online CRM (Flipkart, Amazon, eBay) uses it then you should use Realm Database. It is easier to use and lighter and approximately you can say 10 times faster than SQLite.

Classes & Methods


Example: Using Realm Database in Flipkart App with Code

Now we are going to create a very simple app for Flipkart like Application using Realm database in an Android app.
Application using Realm Database, where we have taken a List of Saris and I will explain you, how we can add new Sari into Flipkart android app using Realm Database.
First, create a New Project in Android Studio from File > new > New Project and Give an App name and click on finish.
Now Go to build.gradle file and add dependencies for Realm Database:

Here we are using Recycler View for showing list of Saris in Flipkart type app, for that you need to add a library in dependencies:

Now add Permission for the Internet in AndroidManifest.xml

Add 4 EditText for adding title, description, Price and Image URL of saris for adding in Flipkart type Sari App. For that, create .xml file named as ‘edit_item.xml’, Add code for all EditText like below code:

Now we should create custom ListView for Displaying Sari’s items with description, price, and Image of Sari like Flipkart App. Create .xml file named as ‘item_saris.xml’, Add code for Image View and Text Views as below:

Now for coding Part, first of all, you have to create get/set methods for title, description, price and imageUrl of saris. Add Sari.java in package ‘com.acadgild.realmsariexample’

Do same for others parts like description, price, and imageUrl, create get/set methods for all.
Now we should create object for RealmConfiguration and extends from the RealmObject class

Add RealmString.java into the same package:
Now you should set values for adding items of saris into the list, you can set some default values for setting values as below:

Next thing is creating RealmManager.java for initializing Realm Configuration and setting Realm Configuration, Add code for Increment /Decrement of the counter for Realm data.
For initializing Realm configuration:

To Set Realm Configuration use below code:

Now you have to add counter to increment item or decrement item from the list:


Now you should create SarisActivity.java for initializing Realm Database Configuration as shown below:

Now it’s time for adding Adapter into the Recycle View, for that add SarisAdapter.java activity as shown below:

Now add Sari’s view into recycler view from the added items, for that add SarisAddView.java as shown below:

Now Add Listener for Saris List Adapter, for that add SarisScopeListener.java class:

Add a code for Save Sari’s new Item, Delete Item from the list or If you want to edit Item from the List of Item, for that add ‘SarisPresenter.java’
For saving a new Sari into a list, use below code:

If you want to delete an item of sari from a list like Flipkart, use below Code:

If you want to edit an item of sari from a list, use below Code, it will update a sari details:

Now your whole work is done. This will be a sari shop like Flipkart and you can add your product using Realm database. Try to resolve errors using debugging techniques if you are getting from your project and try to run your project file.
If still facing any errors or issues reach out to [email protected]
Download Project Link: GitHub

Output:

List of Saris for shopping like Flipkart App

Add New Sari into a List

After Adding Sari into List

Conclusion:

Realm database is an easy way to perform queries and doing transactions asynchronously compare to other internal databases like SQLite in Android. It is an easy way to build queries and very speedy on response timing. It will become the best option of SQLite in coming years for Mobile Applications Development.

One Comment

  1. Thanks for providing a valuable resources for everyone .
    Kindly share a link to sample if possible .
    with regards:
    Abhinav

Leave a Reply

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

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

Related Articles

Close