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
- dependencies {
- classpath 'com.android.tools.build:gradle:3.0.1'
- classpath 'com.google.gms:google-services:3.2.0' }
Gradle for app
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:26.1.0'
- implementation 'com.android.support.constraint:constraint-layout:1.0.2'
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'com.android.support.test:runner:1.0.1'
- androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
- compile 'com.google.firebase:firebase-core:12.0.0'
- compile 'com.google.firebase:firebase-database:12.0.0'
-
- }
- 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
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="io.github.selvaraju_saravanan.firebasedata.MainActivity">
- <Button
- android:id="@+id/sendData"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:text="Data Send"
- tools:layout_editor_absoluteX="148dp" />
-
- </RelativeLayout>
Next, go to app >> src >> java >> Domain Name >> MainActivity.java. Select MainActivity files to follow the code as given below.
- package io.github.selvaraju_saravanan.firebasedata;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
-
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
-
- import java.security.PrivateKey;
-
- public class MainActivity extends AppCompatActivity {
-
-
- // private Firebase mRef;
- private Button mSendData;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- // Firebsae.setAndroidContext(this);
- // mRef = new Firebase("https://my-projects-dcffa.firebaseio.com/");
- mSendData =(Button) findViewById(R.id.sendData);
-
- mSendData.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference myRef = database.getReference("Name");
-
- myRef.setValue("Nikmal");
- }
- });
-
- }
- }
FirebaseDatabase database = FirebaseDatabase.getInstance();
- Database Connectivity
DatabaseReference myRef = database.getReference("Name");
- Create table name, the table name is Name
myRef.setValue("Nikmal");
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.