Android Firebase Realtime Database CRUD Tutorial

Google have announce new Firebase Real-time Database for multiple platform such as Android, IOS and Websites. It is a cloud based hosted database that stored in JSON format and real time synchronize the latest update from the server. Firebase Real-time Database does not need to write server code such as PHP to retrieve the data from the server, its can just retrieve from client application. It is the best choice for developer who dint have server side knowledge or require limited time to develop an application. In this tutorial, I will teach you how to use android firebase realtime database in your mobile application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “FirebaseDatabaseExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add project in Firebase Console

Go to Firebase console https://console.firebase.google.com/  and create a new project. Enter your project name and select which country you live.

firebase console add project

Add firebase to Android App

Inside the project control panel select add firebase to android app. Enter your android project and copy the package name into the firebase console package name section. After that click Add app button.

add pacakge name

Move google-services.json into your project

It will automaticaly download a json file into your computer. Then switch to the Project view in Android Studio to see your project root directory and move the it into your Android app module root directory.

move json file in your project

Edit build.gradle

In the project-level build.gradle add the following line.

After that go to your app-level build.gradle, apply the plugin and add a new dependency

This show insert below the dependency if not it will occur error.

Now, you can synchronize your android project.

Create Custom Alert layout

Open res folder and Right click layout to create a new xml file “custom_alert.xml” for custom alert. After that, change the xml source code to the sample below.

Create a custom list item layout

Create another xml again in your layout folder and name it “custom_list.xml”. Then, modify the xml to the sample below.

Edit activity_main.xml layout

Copy and paste the source code below in your activity_main.xml.

Create a new model class

Right click the package name and create the new class “Data” to add 3 fields key, name and age.

Create a Adapter class

Right click the package name again and follow the sample below to declare a custom adapter for your list.

Edit MainActivity.java class

Go to MainActivity class and this class will be perform Create, Retrieve, Update and Delete Operation for the firebase realtime database. Besides, it also allow to search with specific name.

Run Your Project

At last, now you can run your project in your android device, you can try to delete database record from the firebase console and you will see the real-time instant changes on your devices.

View your Database

Go to your firebase console, you will see “Database” in the left section list. Click it to check your database, it is work anot.

Note : you should change the rules to the following because this sample project we dont use authentication. So we should allow it to access the database to the public.

(Android Firebase Realtime Database)

Source Code

(Visited 3,381 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

1 Response

  1. Danish says:

    you must add the screenshots of the activities in each tutorial

Leave a Reply

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

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email