Kotlin Firestore example – CRUD Operations with RecyclerView | Android

Cloud Firestore helps us store data in the cloud. It supports offline mode so our app will work fine (write, read, listen to, and query data) whether device has internet connection or not, it automatically fetches changes from our database to Firebase Server. We can structure data in our ways to improve querying and fetching capabilities. This tutorial show you an Android app that can do Firestore CRUD Operations with RecyclerView.

Related Post: Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android

I. Technologies

– Android Studio 3
– Kotlin 1.2.0
– Firebase Firestore 11.8.0

II. Overview

1. Goal

We will build an Android App that supports showing, inserting, editing, deleting Notes from/to Cloud Firestore Database with RecyclerView:


Firebase Console for Firestore will be like:


2. Cloud Firestore
2.1 Add Firestore to Android App

– Create Android Project, then go to Firebase Console, create Firebase Project:


– When your app is created on Firebase Console, you will need to register Android Project. Now fill project package name to register your app:


– Download google-service.json config file and follow instructions:


– Add Service plugin for Gradle:


– Open build.gradle (App-level), add dependency:

– We also need Authentication to work with Firestore. To simplify this example without Authentication steps, we will allow anyone be able to read or write to the database:


2.2 Initialize & Reference

2.3 Add Data

Assume that our database structure is like:

– using set() to create or overwrite a single document:

– using add(), Cloud Firestore will auto-generate an ID:

2.4 Get Data

– get a Document from Collection:

– get all Documents from Collection:

2.5 Delete Data

2.6 Get Realtime Updates

When no longer listening to data, we must detach listener so that event callbacks stop getting called:

3. Project Structure


III. Practice

1. Set up Project

– Create New Project with package name com.javasampleapproach.kotlin.firebase.cloudfirestore.
– Add images (found in source code) to drawable.
– Follow these steps to add Firestore to the Project.

2. Layout
2.1 Main Activity

Open res/layout/activity_main.xml file:

2.2 Item Layout

Add item_note.xml layout file to res/layout:

2.3 Activity Layout for Adding/Updating Note

Add activity_note.xml layout file to res/layout:

2.4 Menu

Under res folder, create menu folder and add menu_main.xml:

3. Data Model

Under model package:

4. RecyclerView Adapter Class

To work with RecyclerView, we will:
– create RecyclerView.Adapter subclass
– create ViewHolder class

To support edit Note and delete Note, we register a callback for ivEdit and ivDelete ImageView.

To transfer data between 2 Activity, we use Intent.

Under adapter package:

5. Activity
5.1 Main Activity

To work with RecyclerView, we will:
– set LayoutManager
– set adapter

To work with Add Note button on Menu, we will:
– override onCreateOptionsMenu() function to specify menu layout resource
– override onOptionsItemSelected function to launch NoteActivity

To display List of Items, we will create loadNotesList() and call it inside onCreate().

To update data change, we register a Listener and remove it in onDestroy().

5.2 Note Activity

6. Android Manifest

Define NoteActivity class as an Android Activity:

IV. Source Code


By grokonez | January 10, 2018.

Last updated on July 13, 2018.

Related Posts

5 thoughts on “Kotlin Firestore example – CRUD Operations with RecyclerView | Android”

  1. I’m getting this error trying to run the project in Android Studio on Mac. Anyone know how to fix

    Task ‘assemble’ not found in root project ‘KotlinFirebaseCloudFireStore’.

  2. getting the error ‘NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/firestore/QueryListenOptions;’
    even I update android to androidx?

Got Something To Say:

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