Each 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 file

implementation '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 order

apply plugin: 'kotlin-kapt'

apply plugin: 'io.objectbox'

We start by creating our Entity class, called Note in this case which is a Kotlin data class

import io.objectbox.annotation.Entity

import io.objectbox.annotation.Id

import java.util.*



@Entity

data 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.json

We are good to go.

The build above generated a MyObjectBox class based on our entity and helps setup BoxSore for our project

Next, create a kotlin Singleton called ObjectBox

Boxstore 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.xml

The 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 icon

To 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 List

I 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.xml

The 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.

Summary

Creating a new item,

1. Create a box,

2. Call box put method passing your model class

To retrieve all objects from the database, call box all method

Note 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.