Building a functional Note taking Application with ObjectBox and Kotlin

Building a functional Note taking Application with ObjectBox and KotlinNgenge SeniorBlockedUnblockFollowFollowingApr 17Photo by Markus Spiske on UnsplashAlmost any app you want to build requires storing and retrieving data, and this is applicable even to the simplest of applications you want to build.

On Android, the traditional way of storing data is using the SQLite database.

Thanks to the coming of Android jetpack including Architecture components and Room library, SQLite manipulation has been made better.

I was researching a few weeks ago and fell on a new database called ObjectBox which as described on their site, ObjectBox is a superfast database for IoT and Mobile.

It has bindings for Java, Kotlin, Go, Swift and C.

I have hands-on with it and it is simple to comprehend and get started.

Purpose of PostThe purpose of this post is to get you to know how to use this cool database known as ObjectBox and how to use it to build a fully functional application.

Though it would be a simple note-taking application, there is a lot to learn from this.

Enough of the talk, here is the application we will build composing of two screens.

screens of note-taking applicationEach note is made up of a title, description, title and date which defaults to the current time.

Start by creating an Android Studio application and enable Kotlin support.

Add object box, recyclerview and cardview dependencies in the dependencies section of your build.

gradle fileimplementation 'com.

android.

support:recyclerview-v7:28.

0.

0'implementation "io.

objectbox:objectbox-kotlin:2.

3.

4"implementation 'com.

android.

support:cardview-v7:28.

0.

0'At the top of the file, apply kotlin-kapt and io.

objectbox in the following orderapply plugin: 'kotlin-kapt'apply plugin: 'io.

objectbox'We start by creating our Entity class, called note in this case which is a Kotlin data classimport io.

objectbox.

annotation.

Entityimport io.

objectbox.

annotation.

Idimport java.

util.

*@Entitydata class Note( @Id var id:Long = 0, var title:String?, var longDescription:String?, var updatedAt:Date = Calendar.

getInstance().

time)Note the @Id annotation which is a Long.

Other Id types are possible but let’s keep it simple for now.

At this point, you have to click on Build>Make Project in Android Studio to generate necessary files.

A file will be generated in app/objectbox-models/default.

jsonWe are good to go.

The build above generated a MyObjectBox class based on our entity and helps setup BoxSore for our projectNext, create a kotlin Singleton called ObjectBoxBoxstore is the interface to the db and thus manages Boxes.

Boxes persist and query entities.

Next, create an app class and initialize ObjectBox there and do not forget to add the android: name value as App to your manifest file.

Ok, we proceed by adding a new Activity, called AddNoteActivity.

The associated layout file will compose of two text fields for title and description.

We use in this case, a Material text field composed usually of a TextInputLayout surrounding a TextInputField.

The TextInputLayout helps in floating our hint text.

Here is the xml code.

add_note_layoute.

xmlThe concept is that when the user is done with adding the note title and description, they hit a menu icon(double tick) at the top.

Add a new vector asset in Android studio and search for done.

done iconTo create a new note, we initialize a box of type Note in AddNoteActivity.

kt onCreate method.

Next, we create a method addNote which adds a new note to the database using our noteBox.

private lateinit var notesBox:Box<Note>override fun onCreate(savedInstanceState: Bundle?) { super.

onCreate(savedInstanceState) setContentView(R.

layout.

add_notes_layout) notesBox = ObjectBox.

boxStore.

boxFor(Note::class.

java)}Method addNote gets text from title and description text fields and creates a new note and persists to the db using the notesBox object.

Notice that the put method of the box adds a new note to the database.

Your AddNoteActivity.

kt should look like this at the end.

Displaying the ListI make the assumption that you should be able to create a recyclerview adapter and able to display items in a recyclerview.

Your can check my previous post, Recyclerview Item Click Listener , it should show you how to use a recyclerview.

With that said, our main activity has a recyclerview for displaying our notes list.

Create a method update notes that is called in onCreate and onResume that retrieves all the notes from object box and displays them in your recyclerview.

But first, here is out note_list_item.

xmlThe list item is a Material cardview housing a relative layout with three TextViews for title, description, and date.

Same way like we did before, we create a Box of type note in MainActivity, retrieve all notes and add to our adapter.

private lateinit var notesBox:Box<Note>override fun onCreate(savedInstanceState: Bundle?) { super.

onCreate(savedInstanceState) setContentView(R.

layout.

add_notes_layout) notesBox = ObjectBox.

boxStore.

boxFor(Note::class.

java) updateNotes()private fun updateNotes(){ val notes = notesBox.

all noteListAdapter?.

updateItems(notes) }There it is, we are done here and we have a note taking app.

SummaryCreating a new item,1.

Create a box,2.

Call box put method passing your model classTo retrieve all objects from the database, call box all methodNote that this is not all you can do with object box as it gives you the chance for advanced queries and table relationships like any database.

You can check their website and you will learn more.

Thank you for reading and happy coding.

.

. More details

Leave a Reply