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.
Add Gradle plug-in (build.gradle(app)) at the end of the code.
- apply plugin: 'com.google.gms.google-services'
Add classpath plug-in into project gradle.
- classpath 'com.google.gms:google-services:3.0.0'
Add the dependencies plug-ins into App gradle.
- compile 'com.google.firebase:firebase-database:9.2.0'
- compile 'com.google.firebase:firebase-crash:9.2.0'
- compile 'com.google.firebase:firebase-auth:9.2.0'
- compile 'com.firebase:firebase-client-android:2.3.1'
Now, go to Android Studio and copy the code.
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context="com.example.saravanans.firebaseapp.MainActivity">
-
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPersonName"
- android:hint="Name"
- android:ems="10"
- android:id="@+id/nametxt" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/nametxt"
- android:inputType="number"
- android:hint="Age"
- android:ems="10"
- android:id="@+id/agetxt" />
- <Button
- android:id="@+id/addvalue"
- android:layout_below="@+id/agetxt"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="ADD Data" />
- </RelativeLayout>
MainActivity.java
- package com.example.saravanans.firebaseapp;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import com.firebase.client.Firebase;
-
- public class MainActivity extends AppCompatActivity {
- private Button addvalue;
- public EditText name,age;
- public Firebase ref;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Firebase.setAndroidContext(this);
- ref=new Firebase("https://example-17fdf.firebaseio.com/");
- addvalue=(Button)findViewById(R.id.addvalue);
- name=(EditText) findViewById(R.id.nametxt);
- age=(EditText) findViewById(R.id.agetxt);
- addvalue.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Firebase nameChild=ref.child("Name");
- nameChild.setValue(name.getText()+"");
- Firebase ageChild=ref.child("Age");
- ageChild.setValue(age.getText()+"");
- Toast.makeText(getBaseContext(),"Details Added",Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Here, add your database URL.
- ref=new Firebase("DB URL");
Now go to firebase console choose database in menu and select "Rules" tab update the below code.
- {
- "rules": {
- ".read": "auth == null",
- ".write": "auth == null"
- }
- }
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.