Tuesday, 24 May 2016

FireBase Tutorial for Android

Firebase
Firebase is a platform for building mobile and web application. You can build application quickly with real time data update. Using firebase is very easy and it stores data in JSON format. You do not need to configure your server when you use firebase. Everything will be handled by firebase automatically. So no coding on server side. It will save time and will make you more productive. In this firebase android tutorial we will do a very simple example of using firebase for our android application.
Why we Use Firebase in Android App
  • Easy to Develop the apps and quick to implement.
  • No server side configuration needed. No PHP Scripts and No Database Designs.

 Step 1:
Just go to https://www.firebase.com/
 and Signup for a free plan. You can simply sign in with google.


Step 2 Create App:
--------------------------

Now click on the app you created and you will see your app’s dashboard. Here you will see your data.
Now first you need Internet Permission
    <uses-permission android:name="android.permission.INTERNET" />
come to your build.gradle file
    compile 'com.firebase:firebase-client-android:2.4.0'
You need to add some more lines in this gradle file. After the buildTypes block add the following code.
    //Add the following block
    packagingOptions{
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'

    }
activity_main.xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   
android:orientation="vertical"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:context="com.sgp.anil.firebaseexample.MainActivity"
   
tools:showIn="@layout/activity_main">

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText_Id"
       
android:hint="ID" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText_Name"
       
android:hint="Name" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText_Email"
       
android:hint="Email" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText_Address"
       
android:hint="Address" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Save"
       
android:id="@+id/button_save"
       
android:layout_gravity="center_horizontal" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Show"
       
android:id="@+id/button_show"
       
android:layout_gravity="center_horizontal" />
</
LinearLayout>

 Config.Java
-----------------
package com.sgp.anil.firebaseexample.firebase;

/**
 * Created by Anil on 5/24/16.
 */
public class Config {
   
public static final String FIREBASE_URL = "https://samplemyapp.firebaseio.com/";
}
 Student.Java
----------------------
package com.sgp.anil.firebaseexample.Model;

/**
 * Created by Anil on 5/24/16.
 */
public class Student {
    String
id;
    String
name;
    String
email;
    String
address;

   
public String getId() {
       
return id;
    }

   
public void setId(String id) {
       
this.id = id;
    }

   
public String getName() {
       
return name;
    }

   
public void setName(String name) {
       
this.name = name;
    }

   
public String getEmail() {
       
return email;
    }

   
public void setEmail(String email) {
       
this.email = email;
    }

   
public String getAddress() {
       
return address;
    }

   
public void setAddress(String address) {
       
this.address = address;
    }
}
MainActivity.Java
------------------------
package com.sgp.anil.firebaseexample;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.firebase.client.Firebase;
import com.sgp.anil.firebaseexample.Model.Student;
import com.sgp.anil.firebaseexample.firebase.Config;


public class MainActivity extends AppCompatActivity {
   
private EditText eid, edname, edemail, edadd;
   
private Button buttonSave, buttonshow;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

        Firebase.setAndroidContext(
this);

       
buttonSave = (Button) findViewById(R.id.button_save);
       
buttonshow = (Button) findViewById(R.id.button_show);
       
eid = (EditText) findViewById(R.id.editText_Id);
       
edname = (EditText) findViewById(R.id.editText_Name);
       
edemail = (EditText) findViewById(R.id.editText_Email);
       
edadd = (EditText) findViewById(R.id.editText_Address);

       
buttonSave.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
//Creating firebase object
               
Firebase ref = new Firebase(Config.FIREBASE_URL);

               
//Getting values to store
               
String id = eid.getText().toString().trim();
                String name =
edname.getText().toString().trim();
                String email =
edemail.getText().toString();
                String address =
edadd.getText().toString();
                Student student =
new Student();


               
//Saving values to firebase
               
student.setId(id);
                student.setName(name);
                student.setEmail(email);
                student.setAddress(address);
                ref.child(
"Student").setValue(student);

                Snackbar snackbar = Snackbar.make(v,
"Saved", Snackbar.LENGTH_LONG);
                snackbar.show();


            }
        });
       
buttonshow.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                startActivity(
new Intent(getApplicationContext(), ShowingActivity.class));
            }
        });
    }
}
 showing_activity.xml
-----------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context="com.sgp.anil.firebaseexample.ShowingActivity">

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView_id"
       
android:layout_gravity="center_horizontal" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView_name"
       
android:layout_gravity="center_horizontal" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView_email"
       
android:layout_gravity="center_horizontal" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView_add"
       
android:layout_gravity="center_horizontal" />
</
LinearLayout>

ShowingActivity.Java
------------------------------
package com.sgp.anil.firebaseexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.sgp.anil.firebaseexample.Model.Student;
import com.sgp.anil.firebaseexample.firebase.Config;

public class ShowingActivity extends AppCompatActivity {
    TextView
tid, tname, tem, tadd;


   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_showing);
       
tid = (TextView) findViewById(R.id.textView_id);
        
tname = (TextView) findViewById(R.id.textView_name);
       
tem = (TextView) findViewById(R.id.textView_email);
       
tadd = (TextView) findViewById(R.id.textView_add);

        Firebase ref =
new Firebase(Config.FIREBASE_URL);

        ref.addValueEventListener(
new ValueEventListener() {
           
@Override
           
public void onDataChange(DataSnapshot dataSnapshot) {
               
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                   
//Getting the data from snapshot
                   
Student person = postSnapshot.getValue(Student.class);

                   
//Adding it to a string
                   
String id = "Id: " + person.getId();
                    String name =
"Name: " + person.getName();
                    String email =
"Email: " + person.getEmail();
                    String add =
"Address: " + person.getAddress();

                   
//Displaying it on textview
                   
tid.setText(id);
                   
tname.setText(name);
                    
tem.setText(email);
                   
tadd.setText(add);

                }

            }

           
@Override
           
public void onCancelled(FirebaseError firebaseError) {
   Log.e("FireBase Error",""+firebaseError.toString());
            }
        });
    }
}
Output
----------------












No comments:

Post a Comment