Before Going to this tutorial hope you followed the Part I for connecting firebase to android and if not than click here.
Go to your firebase console where you have your project.
On the left panel you can see the following things.
Analytics:- Shows app usage and user engagement with your app.
Auth:-Stores User's list that are registered by your app.
Database:- Stores app's data which is similar to json view.
Storage:- Stores Media files.
Hosting:-Uploading Web Apps.
Admobs:- Provides adds for android app. and etc
Steps to implement Firebase Database in your Android App.
- Add App level dependency to your build.gradle
dependency{
compile 'com.google.firebase:firebase-core:11.0.1'
compile 'com.google.firebase:firebase-database:11.0.1'
compile 'com.firebase:firebase-client-android:2.3.1'
}- Now Sync your project if you are facing some errors like below please click here.

- Now take a button in activity_main.xml, when user click's on the button it should add some static value to the firebase database.
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eazy.firstfireapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Data to Firebase"
android:id="@+id/btnsave" />
</LinearLayout>
- In the MainActivity initialize the Firebase by passing the database url which is given in the firebase console. Go to Left panel in console and select database there you will find your url.

- Go to the Rules tab. According to the below rule only authenticated users can read and write the database, but here we haven't taken any auth yet, so change it to null and publish.


- Go to the Data Tab Again. Consider this an Object.

- Now You can add child to that Object which is in key and Value pair. But we need to add this via Android app.

- Full Code For MainActivity.java
package eazy.firstfireapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.firebase.client.Firebase;
public class MainActivity extends AppCompatActivity {
Firebase mref;
Button btnsave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
// pass your database link
mref=new Firebase("https://firstfireapp-ed41b.firebaseio.com/");
btnsave= (Button) findViewById(R.id.btnsave);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//add child object to parent object
Firebase mrefchild=mref.child("Name");
// set the value of Child object
mrefchild.setValue("Marshmallowz");
}
});
}
}
- Now try to execute the Project, If you are facing any error while running your app than Click Here and if your app is running properly than click on that button. It will Save the data to the firebase database.

- Output in Firebase Console

[…] Next i.e Part II we’ll start with Database using […]
ReplyDeletecan u plz upload registration and login form using firebase... I hope u will work on it very soon..
ReplyDelete