Insights from Our Experts

Blog image

Using Firebase Realtime Database in Android

Author Image

Eby Cloudins,Software Engineer


Firebase is an open source platform for building mobile and web applications. It can provide real-time data updates with great ease. Firebase stores the data in JSON format. We don't need to put any efforts in configuring the server, the Firebase will handle everything automatically and saves a big span of time and increase the productivity.

How it works in real-time

When the real-time data is updated, it stores the data in the cloud and simultaneously notifies all the connected devices within milliseconds.

How it works in offline

Whenever the connection is lost the database STK uses a local cache on the device to serve and store changes and once the user comes back online their local data is automatically synchronized.

How to keep the data secure

For security, we can specify who has the access to what piece of data. The security rules are securely stored within in real-time database on the server

Adding Firebase to Android project

  • First, we need to create a Firebase account for free plan to gain access to their console.
  • Now open the Android Studio and create a new project. Go to Tools  - Firebase . This will take you to Firebase assistant window. Select real-time database to set up the database.

  • Follow the Steps to configure Firebase in the project.  This will add all the dependencies in Gradle automatically.

Adding data into Firebase database

  • First, create a model class for the data you need to add to the database. Create a model class object with data as parameters.
ModelClass modelClassObject = new ModelClass(data 1, data 2,..);
  • Then create a database instance.
FirebaseDatabase databaseInstanceName = FirebaseDatabase.getInstance();
  • Now create a node and add the data to it by using .child() and .setValue() method.
databaseInstanceName.child(“new node”).setValue(modelClassObject);


This will create a child node in the database with name “new node” with data as its child.

  • We can also add data directly in the Firebase console.
  • Once this is done, run the program and check your Firebase dashboard. You can see the data added.

Retrieving data from Firebase database

  • We can retrieve data from Firebase in real time without gcm.
  • For retrieving, use the ValueEventListener() method.
databaseInstanceName.addValueEventListener(new ValueEventListener() {
   public void onDataChange(DataSnapshot snapshot) {        
   for (DataSnapshot postSnapshot : snapshot.getChildren()) {
    ModelClass modelClassObject  = postSnapshot.getValue(ModelClass.class);          }        


  • Use the model class object to access the data.

Refer the following example to create a simple chat room by using firebase.

Removing data from Firebase database

  • Use Firebase instance and the name of the child to delete the data.
databaseInstanceName.child(“new node”).removeValue();


This will delete the child named “new node”

Sample program to implement Firebase in Android

For understanding how to apply this in a project, you can refer this example.
So you have seen how easy it is to set up a database and manage your data with Firebase. Firebase has many more applications. You can learn more about Firebase from this link.

I Need

Help for