Real-Time Data Storage In Firebase Using Android Studio

Introduction

 
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 'com.android.tools.build:gradle:3.0.1'  
  3.                         classpath 'com.google.gms:google-services:3.2.0' }  
Gradle for app
 
 
  1. dependencies {  
  2.     implementation fileTree(dir: 'libs', include: ['*.jar'])  
  3.     implementation 'com.android.support:appcompat-v7:26.1.0'  
  4.     implementation 'com.android.support.constraint:constraint-layout:1.0.2'  
  5.     testImplementation 'junit:junit:4.12'  
  6.     androidTestImplementation 'com.android.support.test:runner:1.0.1'  
  7.     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'  
  8.     compile 'com.google.firebase:firebase-core:12.0.0'  
  9.     compile 'com.google.firebase:firebase-database:12.0.0'  
  10.   
  11. }  
  12. apply plugin: 'com.google.gms.google-services'  
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="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/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" />  
  16.   
  17. </RelativeLayout>  
Next, go to app >> src >> java >> Domain Name >> MainActivity.java. Select MainActivity files to follow the code as given below. 
  1. package io.github.selvaraju_saravanan.firebasedata;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7.   
  8. import com.google.firebase.database.DatabaseReference;  
  9. import com.google.firebase.database.FirebaseDatabase;  
  10.   
  11. import java.security.PrivateKey;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.   
  15.   
  16.   //  private Firebase mRef;  
  17.     private Button mSendData;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.      //   Firebsae.setAndroidContext(this);  
  25.        // mRef = new Firebase("https://my-projects-dcffa.firebaseio.com/");  
  26.         mSendData =(Button) findViewById(R.id.sendData);  
  27.   
  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");  
  33.   
  34.                 myRef.setValue("Nikmal");  
  35.             }  
  36.         });  
  37.   
  38.     }  
  39. }  
FirebaseDatabase database = FirebaseDatabase.getInstance();
  • Database Connectivity 
     
    DatabaseReference myRef = database.getReference("Name");
  • Create table name, the table name is Name 
     
    myRef.setValue("Nikmal");
  • 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.
 
 
 
Output
 
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.
 
Example
 
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.


Similar Articles