Connecting Android App With Google Firebase Database

Firebase
  • Firebase is a backend tool for mobile and Web Applications.
  • It offers real-time databases, API’s, multiple authentication types and hosting platforms. 
  • Google Firebase helps to develop the Applications rapidly.

Requirements

  • Android Studio.
  • Google account.

 Follow the steps given below to build an Android Application

Open an Android Studio and create a new project.

Login to Google Firebase and create a new project in console.
  
   
After creating the project, select Add Firebase to your Android app in Firebase overview page.
 
 

After adding an Application, give your Android Application package name, app nickname and debug signing certificate SHA-1 and hit REGISTERAPP.

 

Now, download Google-Services.json file and add into your Application; app folder.

Capture.JPG

Add Gradle plug-in (build.gradle(app)) at the end of the code.
  1. apply plugin: 'com.google.gms.google-services'   

Add classpath plug-in into project gradle. 

  1. classpath 'com.google.gms:google-services:3.0.0'  
Add the dependencies plug-ins into App gradle.   
  1. compile 'com.google.firebase:firebase-database:9.2.0'  
  2. compile 'com.google.firebase:firebase-crash:9.2.0'  
  3. compile 'com.google.firebase:firebase-auth:9.2.0'  
  4. compile 'com.firebase:firebase-client-android:2.3.1'  

Now, go to Android Studio and copy the code.

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:id="@+id/activity_main"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     android:paddingBottom="@dimen/activity_vertical_margin"  
  12.     tools:context="com.example.saravanans.firebaseapp.MainActivity">  
  13.   
  14.     <EditText  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:inputType="textPersonName"  
  18.         android:hint="Name"  
  19.         android:ems="10"  
  20.         android:id="@+id/nametxt" />  
  21.     <EditText  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@+id/nametxt"  
  25.         android:inputType="number"  
  26.         android:hint="Age"  
  27.         android:ems="10"  
  28.         android:id="@+id/agetxt" />  
  29.     <Button  
  30.         android:id="@+id/addvalue"  
  31.         android:layout_below="@+id/agetxt"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="ADD Data" />  
  35. </RelativeLayout>   

MainActivity.java

  1. package com.example.saravanans.firebaseapp;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.Toast;  
  9.   
  10. import com.firebase.client.Firebase;  
  11.   
  12. public class MainActivity extends AppCompatActivity {  
  13.     private Button addvalue;  
  14.     public EditText name,age;  
  15.     public Firebase ref;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         Firebase.setAndroidContext(this);  
  21.         ref=new Firebase("https://example-17fdf.firebaseio.com/");  
  22.         addvalue=(Button)findViewById(R.id.addvalue);  
  23.         name=(EditText) findViewById(R.id.nametxt);  
  24.         age=(EditText) findViewById(R.id.agetxt);  
  25.         addvalue.setOnClickListener(new View.OnClickListener() {  
  26.             @Override  
  27.             public void onClick(View view) {  
  28.                 Firebase nameChild=ref.child("Name");  
  29.                 nameChild.setValue(name.getText()+"");  
  30.                 Firebase ageChild=ref.child("Age");  
  31.                 ageChild.setValue(age.getText()+"");  
  32.                 Toast.makeText(getBaseContext(),"Details Added",Toast.LENGTH_SHORT).show();  
  33.             }  
  34.         });  
  35.     }  
  36. }   

Here, add your database URL. 

  1. ref=new Firebase("DB URL");  



Now go to firebase console choose database in menu and select "Rules" tab update the below code.

  1. {  
  2.   "rules": {  
  3.     ".read""auth == null",  
  4.     ".write""auth == null"  
  5.   }  
  6. }   

Please don’t forget to add Internet access permission on androidmanifest.xml.

Now, run your Application. Give the name and age, when you press ADD DATA button and the data will insert into the database.

Sample output




Summary

In this article, we discussed about what Google Firebase is, and how to connect an Android app with Firebase DB. Also, we saw how to insert some values into the database.

Up Next
    Ebook Download
    View all
    Learn
    View all