Firebase example in Android

Firebase is a mind blowing technology  developed by Google.it’s is a cloud services.which have lot’s of features .
In this article let’s get start with Realtime Database Feature of Firebase.


Realtime Database

Firebase Database supports android ,ios and web  platforms . its a cloud storage non-sql database technique  . data’s are stored as JSON .synced across connected devices in milliseconds, and available when your app goes offline. that means data transmission between server and client will be more faster than before . if change any vlaue  in database . for example customer name or age. it will appear on client screen Real time( with out any delay) or no need to refresh the page.
in this Article i show you some basics of Firebase  in android with the help of small application .its very easy to use firebase with android . 
  • Create a new Project in your android studio 
  • if everything is okey . Right click on app folder Open module Settings>Cloud and tick on firebase check box and press okey . android studio will add the firebase dependency in you project 

  • then add packaging Options directive to your build.gradle file: 


 android {  
...
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
}

After all this your build.gradle will be look like this

  • Create a new java class FirebaseExample.java  extend form Application in java>src  and copy past following code .
 import android.app.Application;  
import com.firebase.client.Firebase;
/**
* Created by dhanish on 6/30/2016.
*/
public class FirebaseExample extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
  • Give internet permission in AndroidManifest.xml and write  android:name attribute in <application> tag .shows in following screenshot 

Now We done all basic configuration .Next we are going to create small Weather application to learn how Firebase database is working .

  • We have to create a firebase database for that go to https://console.firebase.google.com .login with google username and password .then Create New Project >Enter the project name (your choice)  eg:-WeatherMe.> press Create button
  • Go to project>Database>Press Add Button to create new child in root .

  • Go to Rule > Give read and write permission .this is very important without permission app can’t access the data

the firebase database creation is almost finished next we have to develop our app

  • For mobile app create following layout 

app have two Button Foggy and sunny and one text area .in this text area we data directly from our firebase database which we created  .

when press foggy button app change value in firebase database field Nowday value sunny to foggy same time app change the value in textarea without any refresh .  vice versa when press Sunny.

this is how we connect our app to firebase database

 Firebase mRef;  
mRef=new Firebase("https://weatherme-78199.firebaseio.com/Nowday");

this is database URL https://weatherme-78199.firebaseio.com/Nowday. we can copy this URL from firebase console .Nowday is the field name .

mRef is a firebase object

next we have to set a event Lister for mRef ,if any changes happens in Nowday field value .we need to dispaly this value in our text area result

 mRef.addValueEventListener(new ValueEventListener() {  
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text=dataSnapshot.getValue(String.class);
result.setText(text);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});

this line of code  String text=dataSnapshot.getValue(String.class);  helps to get data from field Now day

set value to text area result.setText(text);

  • this is how we update data in Nowday field .

 suuny.setOnClickListener(new View.OnClickListener() {  
@Override
public void onClick(View v) {
mRef.setValue("SUNNY");
}
});
foggy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRef.setValue("Foggy");
}
});

when press button sunny or foggy update data

 mRef.setValue(“Foggy”);  

  • check full code MainActivity.java

package sa.hod.dhanish.firebase_sample;
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;
import com.firebase.client.DataSnapshot;import com.firebase.client.Firebase;import com.firebase.client.FirebaseError;import com.firebase.client.ValueEventListener;
public class MainActivity extends AppCompatActivity {

TextView result; Button suuny,foggy;Firebase mRef;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); }




public void init(){
result= (TextView) findViewById(R.id.textView); suuny= (Button) findViewById(R.id.SUNNY); foggy= (Button) findViewById(R.id.FUGGY); mRef=new Firebase("https://weatherme-78199.firebaseio.com/Nowday"); mRef.addValueEventListener(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
String text=dataSnapshot.getValue(String.class); result.setText(text); }

@Override public void onCancelled(FirebaseError firebaseError) {

}
});


suuny.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
mRef.setValue("SUNNY"); }
}); foggy.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
mRef.setValue("Foggy"); }
}); }


}

Thank you.

Reply

8 + 7 =