Android Realm Tutorial.
In this piece we explore Realm database, looking at it’s introduction as well as several examples.
What is Realm database?
It is a relatively new,fast, open source embedded database.
Hence this makes realm an alternative to SQLite and Core Data.
It’s is one of those file-based databases that is quick rising in alternative. This is especially due to the current explosion in mobile apps and app development.
Realm database supports several platforms like:
- Android OS
- iOS
- Xamarin.
- React Native
- Desktop applications etc.
It is licensed under Apache license.
Realm was created by Alexander Stigsen together with Bjarne Christiansen under the name TightDB in 2010. They formed a company in 2011 in Y Combinator.
in 2014, they renamed it to Realm. In March 2015, Realm was funded to the tune of about $20 million.
The first version was announced in June 2016.
Why Realm Database?
Realm is a NoSQL soultion. This means it does allow data storage and retrieval that is different from the common tabular relations used in SQL databases. Such databases have become popular for their usage in big data and realtime environments.
Realm has configurable durability. It also allows us to share the same groups of data across multiple processes, but also even multiple devices and clusters.
According to their official website, Realm’s zero-copy design makes it much faster than an ORM(Object Relational Mapper), and often even faster than raw SQLite.
Oclemy
Android Realm – GridView – Write,Read,Show
Android Realm GridView Tutorial and Example.
This is Realm CRUD tutorial with GridView as our adapterview.
In this tutorial we shall see how to first write data into a Realm database from an edittext.
We show a Material Dialog with EditText and button.User clicks the save button after typing into the edittext and we save the data into Realm database.
Then when the user clicks retrieve button we read data from Real database and fill an arraylist.This arraylist we shall then bind to our GridView.
First we need to add the Realm dependency in our build.gradle :
Once we have our dependency,we shall create our data object class :
Thats our Spacecraft model class with two properties.The next thing is we shall craft a simple RealHelper class to assist us in performing our Real CRUD operations.Saving/Writing to Realm,Reading/Retrieving then filling our arraylist. Here’s the RealHelper CRUD class :
Finally we shall have our MainActivity class.The class shall display our input dialog to the user whenever he clicks the FloatingActionButton.He types then we pass the typed data into RealHelper class for it to be inserted to Realm database.We shall also bind our data to GridView via an adapter in this class.
Conclusion
Look,weĀ have seen how to work with Realm data in our android app.First we’ve seen how to save data to Realm,then retrieve that data on button click,filling an arraylist.Which of course we bind to our GridView to be displayed.The source code is above for download.
Download it,extract and import to your android studio.You can change the Realm version in our dependencies. If you prefer video tutorial and step by step explanation with demo then watch one here : https://www.youtube.com/watch?v=BrGO9eXsAH8
Download
Oclemy
Android Realm CRUD – Spinner – Save,Retrieve,Show
Realm Spinner Tutorials and Examples
How to perform basic CRUD in Realm with Spinner as our widget.
Hello this is a simple realm tutorial to perform basic CRUD.WeĀ first write data to Realm database from a simple dialog with edittext,then read that data and show in a simple spinner. Here’s the thing :
Realm is a fast mobile database that allows us to write database code in pure Java.
This gives us type safety and provides for greater flexibility than writing SQL code.
Type safety allows us easily catch errors at compile time.
This is a realm database tutorial with spinner.
Spinner is an android adapterview that allows us display items in a dropdown manner.
In this tutorial we save data to Realm database, retrieve data and show in a Spinner.
Let’s go.
1. Create Basic Activity Project
3. Create User Interface
User interfaces are typically created in android using XML layouts as opposed by direct java coding.
This is an example fo declarative programming.
Here are our layouts for this project:
(a). activity_main.xml
(b). content_main.xml
This layout gets included in your activity_main.xml.
The adapterview we use is a spinner so add spinner in your layout.
(c). input_dialog.xml
This is our input dialog.
This dialog will be shown as our entry form.
Users will enter save data to realm database via this dialog.
It basically comprises edittext and button.
Java Classes
Let’s now come to our Java classes.
Our Realm classes
Here are our Realm database classes.
(a). Spacecraft.java
This class is both our model class as well as our Realm model class.
This means that Realm will create a table for this class.
For that to happen we have to derive from
RealmObject
class.(b). RealmHelper.java
This is like our CRUD class.
It’s here that we actually save data to realm database and retrieve data as well.
Our Activity class
(a). MainActivity.java
This is our Activity class.
Activity is an android component representing User interface.
Our activity will host a spinner and a dialog that acts as our input form.
Download
Oclemy
Android Realm – ListView -Read,Write,Show
Android Realm ListView tutorial and Example.
How to perform CRUD operations agains realm and a listview.
In this tutorial,we shall explore how to work with Realm database.The component of choice is ListView of course.Realm is a fast alternative database to SQLite written in C++.We shall use it to store our data locally. in our device.
First we write or save data to Realm from a material edittext.We the retrieve this data and fill to an array list which we in turn bind to a ListView component.
Source Code
First we shall need to add the dependency for our Realm database in our build.gradle.You can check the latest version of Realm and add in your project of course.
Then we have our Spacecraft class here.This is our POJO class.We specify the Spacecraft properties.Each property shall correspond to a Column in the database. Of course you can see we derive from RealmObject.This class therefore shall correspond to our Realm database table.
We’ll need a class to help us in performing our CRUD activities.Lets create the RealHelper class to help us in saving,retrieving data to and from our Realm database.
Finally we have our MainActivity.Here we shall first create and display our Input Dialog with editexts and buttons.The inputted data shall get passed to our RealHelper class to be savedĀ to database.Moreover we shall bind data to our GridView via an adapter right here.
Conclusion
We have seen how to save data,retrieve data from Realm database and bind the data to a ListView.Thats our Realm ListView tutorial.You can find the complete source code to check for stuff like layouts above.
Just download it,extract and import to your android studio. Moreover,the youtube version of this tutorial for more explanations and demo is below.Subscribe over there as we do post videos regularly. https://www.youtube.com/watch?v=m4MA-Kd_h0E
Download
Oclemy
Android Realm – RecyclerView – Save,Retrieve,Show
Android Realm RecyclerView tutorial
Android Realm RecyclerView Tutorials and Examples.
Today we look at how to work with Realm database alongside a RecyclerView component with CardView.The fisrt thing we do is save/write data to Realm database in our android application.We do this from an edittext.We display to the user a material edittexts inside an input dialog.
He enters the data,clicks save and then we save the data. We also retrieve data from the Realm database and fill an arraylist.This arraylist we then pass t our RecyclerView adapter and bind it as our dataset to the RecyclerView.The RecyclerView consists of CardViews.
OverView
Source Code
The first thing is to add our Realm dependency via our build.gradle,app level.You may want to make sure you have a later version than I used here .
Then we can define our data object,our model class as below :
Now you can see that we derive from RealmObject.This model class shall correspond to our database table.
Lets see our RealmHelper,the class that shall be responsible for all CRUD operations in our project.Here we save to Realm database,retrieve data and fill an arraylist.
Next we are using a RecyclerView so we need an adapter :
The purpose of our Adapter class is to receive a List of data and bind it to our RecyclerView.We have a recyclerView.ViewHolder subclass that you can view in the full source code reference download at the top of this page.
Lets have a look at our MainActivity class.First we are going to show our input dialog that has the edittexts to input data and button to save.
We shall pass the typed data to our RealmHelper class for it to be saved to database. We are also going to bind our data to our RecyclerView via an adapter.
Conclusion
Look we’ve looked at how to save data to Realm database,read that data and bind to a recyclerView.That’s it for today.The full source code can be downloaded above.Download it,extract it and import to your android studio. Cheers.
Video
If you prefer more step by step explanations then you can check the video tutorial below.Or watch the demo here.
Download