Firebase Realtime Database Android CRUD Operations in Kotlin

firebase-realtime-database-android

Hey guys, here is the complete Firebase Realtime Database Android with Kotlin guide. Firebase realtime database is NoSQL database which means we don’t have the traditional columns and rows.

In firebase realtime database we can store data in the form of JSON tree structure like below.

firebase-realtime-database-android

In this post we will create an app in which we will perform the CRUD operation of firebase realtime database (Create, Read, Update and Delete).

Firebase Realtime Database Video Tutorial

If you are more comfortable in learning with video tutorials, then here is the tutorial playlist of Firebase Realtime Database Android Kotlin.

Firebase Realtime Database CRUD Operations

First of all create a new android project and then you will need to connect your android with firebase to use firebase realtime database in your android project.

Below is the detailed video tutorial of How to create new Firebase project and how to connect that project with your android project.

After creating and connecting firebase project with android project go to android studio and open activity_main.xml, inside activity_main.xml file paste the following code.

Now open MainActivity.kt and paste the following code.

 

After these changes in MainActivity, let’s create 3 more activities.

  • InsertionActivity (for inserting employee details)
  • FetchingActivity (for fetching employee name only)
  • EmployeeDetailsActivity (for showing employee full details)

In EmployeeDetailsActivity we will also add 2 buttons for Updating & Deleting employee record but we will do it later in this post.

InsertionActivity

Open activity_insertion.xml and paste the following code.

 

Now go to InsertionActivity.kt and paste the following code.

 

After these changes run you app and you will be able to store data in firebase realtime database.

FetchingActivity

In order to fetch data from firebase realtime database and show in a recyclerview we need to create a recyclerview item layout, so let’s do it.

Create a new Layout Resource File in res->layout, I will name it emp_list_item you can name it anything you want.

After creating new Layout Resource File paste the following code there.

 

As we will fetch the Employee details from firebase realtime database, so we will need a data class for Employee.

Create new Kotlin data class and name it EmployeeModel and paste the following code there.

 

To work with RecyclerView in android we need an Adapter class, so create a new Kotlin class and name it EmpAdapter or any name you want.

After creating Adapter class paste the below code there.

 

Now go to activity_fetching.xml and paste the below code there.

 

So the activity_fetching.xml file is set, now go to FetchingActivity.kt and paste the following code there.

 

EmployeeDetailsActivity

Open activity_employee_details.xml and paste the below code there.

 

As you can see in the above xml file we have 2 buttons (btnUpdate & btnDelete) we will use these buttons for updating and deleting data from firebase realtime database.

But first of all we will initialize all the views and we will set employee data to the views.

We are passing employee data from the FetchingActivity.kt, so paste the below code in EmployeeDetailsActivity.kt.

 

Updating Record

For updating data in database we will show a dialog to the user, so create new resource file in the layout directory and name it update_dialog and paste the below code there.

 

Now inside onCreate() method of EmployeeDetailsActivity.kt set a click listener on btnUpdate and call openUpdateDialog() method inside the btnUpdate click listener like below.

 

In the above code when user click on btnUpdate the dialog will be displayed and user can then change the data and when user

click the update button in dialog we will update the Employee data by using empId as unique key identifier.

Deleting Record

For deleting the record add a click listener on btnDelete and inside the

click listener call the deleteRecord() method and pass empId as a unique identifier to delete the specific record.

 

EmployeeDetailsActivity Complete Code

 

So guys at this point our tutorial is completed and in the above steps we

performed the CRUD operations of Firebase Realtime Database with Kotlin.

Happy Coding…

Written by Hilal Ahmad

One Comment

Leave a Reply

Leave a Reply

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