Real-Time Data Storage In Firebase Using Android Studio


This article demonstrates how to store real-time data in Firebase using Android Studio. Also, you will see how to send the new Firebase Data to your real-time data server.

Real-Time Database 

Store and synchronize data with our NoSQL cloud DB. Data is synced across all clients in real-time. The Firebase real-time database is a cloud-hosted database. Data is stored as JSON.
Step 1
Create a new project in Android Studio from File >> Project and fill in all the necessary details.
Step 2
Create a new project in Firebase console.
Enter your application package name. The Nick Name and Debug certificate SHA-1 are optional.
You need to download a google-services.json file and just move the downloaded google-services file into the Android app.
Click on Database in your Project.
After that, the Database appears and select the "Real-time Database Get Started" button.
Security Rules for real-time database Select strat in test mode
Step 3
Next, go to Gradle Scripts >> build.gradle (Module: app). Select build.gradle.
The app gradle compiles the code, and then the build types will appear.
Just replace that with the following code and add the Firebase Core and Firebase Database Gradle to your project, or you can also use Gradle.
Dependencies Class
  1. dependencies {  
  2.                         classpath ''  
  3.                         classpath '' }  
Gradle for app
  1. dependencies {  
  2.     implementation fileTree(dir: 'libs', include: ['*.jar'])  
  3.     implementation ''  
  4.     implementation ''  
  5.     testImplementation 'junit:junit:4.12'  
  6.     androidTestImplementation ''  
  7.     androidTestImplementation ''  
  8.     compile ''  
  9.     compile ''  
  11. }  
  12. apply plugin: ''  
Step 4
Next, go to app >> manifest >> AndroidManifest.xml. Select the Manifest file to Enable INTERNET access. Next, go to app >> res >> Layout >> Activity_Main.xml. Select Activity_Main files to follow the code as given below.
Activity_Main.xml Code 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android=""  
  3.     xmlns:app=""  
  4.     xmlns:tools=""  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="io.github.selvaraju_saravanan.firebasedata.MainActivity">  
  8.  <Button  
  9.         android:id="@+id/sendData"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerVertical="true"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:text="Data Send"  
  15.         tools:layout_editor_absoluteX="148dp" />  
  17. </RelativeLayout>  
Next, go to app >> src >> java >> Domain Name >> Select MainActivity files to follow the code as given below. 
  1. package io.github.selvaraju_saravanan.firebasedata;  
  3. import;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  8. import;  
  9. import;  
  11. import;  
  13. public class MainActivity extends AppCompatActivity {  
  16.   //  private Firebase mRef;  
  17.     private Button mSendData;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  24.      //   Firebsae.setAndroidContext(this);  
  25.        // mRef = new Firebase("");  
  26.         mSendData =(Button) findViewById(;  
  28.         mSendData.setOnClickListener(new View.OnClickListener() {  
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 FirebaseDatabase database = FirebaseDatabase.getInstance();  
  32.                 DatabaseReference myRef = database.getReference("Name");  
  34.                 myRef.setValue("Nikmal");  
  35.             }  
  36.         });  
  38.     }  
  39. }  
FirebaseDatabase database = FirebaseDatabase.getInstance();
  • Database Connectivity 
    DatabaseReference myRef = database.getReference("Name");
  • Create table name, the table name is Name 
  • Parameter Values 
Step 5
Next, go to Android Studio and deploy the application. Select Emulator or your Android Device with USB debugging enabled. Give it a few seconds to make installations and set permissions.
When you click on the data send button, the data will be stored in the Firebase cloud service.
After that, again, go back to Console Firebase > > OverView >> Database >> Real-time Database >> Data. Select the Data and store all kinds of information. But there are some rules. If the rules are true then data will be stored otherwise it will not store any data.
Table Name "Name"
Values "Nikmal"
We have successfully created Real-time Datastore in Firebase app.
Refer to my previous article StreamView Your Location Using Android Studio And InstanceID Token to get started with the basics of Firebase.