MAD Firebase
MAD Firebase
➢ Firebase Introduction
➢ Creating Firebase Project
➢ Linking project with Firebase
You can integrate Firebase services in your Android app directly from
Android Studio using the Assistant window. Make sure you have installed
Google Repository version 26 or higher, with following steps:
Before finally synchronizing the project, you will need to add the google-
services.json configuration file downloaded from Firebase console. The
steps to create and download this file are mentioned next.
2. Second step is Google Analytics for your Firebase project. You can
either leave it default enabled or disable it and click Continue. If you
disable the Analytics, the third step will not appear and the button will be
Create project. In our case, we will just disable the Analytics and create
project.
3. If you enabled Analytics, the third step asks your Country and terms and
conditions of Analytics. Accept these terms and click Create project.
Finally, press
"Sync now" in the
bar that appears
in the IDE. This
will take some
time. Click next
to continue.
Here a possible error may occur: No matching client found for package
name 'com.example.shan.firebasedemoapp1'
This is because the package name of the Firebase project did not match the
package name of the Android App.
Step 4- Finishing:
On the last step, click Continue to console to finish adding the project.
Store and sync data with Firebase NoSQL cloud database. Data is synced
across all clients in realtime, and remains available when your app goes
offline. The Firebase Realtime Database is a cloud-hosted database. Data is
stored as JSON and synchronized in realtime to every connected client.
When you build cross-platform apps with our iOS, Android, and
JavaScript SDKs, all of your clients share one Realtime Database instance
and automatically receive updates with the newest data.
We are going to use RecyclerView to list all the task. An EditText widget
is used to add new task when a button is clicked. All the task store in
Firebase real-time database is retrieved and bind to the RecyclerView
through a RecyclerView adapter. A delete icon is used to delete the task
from Firebase database.
{
“rules”: {
“.read”: true,
“.write”: true
}
}
Or open the project created. In the left panel click Develop and then
Database.
Locate Realtime
Database and click
Create database. You
will see Security rules
for Realtime Database.
Select Start in test
mode and click Enable
for now. This is a
public access to the
database so anybody
can get access to your
database. Although this is security risk but for testing, it is OK.
Program:
We will create a to do task application with backend of Firebase Real-time
database. The main Firebase classes include FirebaseDatabase,
DatabaseReference and DataSnapshot.
listing 1 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/add_task_box"
android:layout_marginBottom="12dp"
android:orientation="vertical"
/>
<EditText
android:id="@+id/add_task_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:inputType="text"
android:layout_marginStart="86dp"
android:layout_marginBottom="79dp"
android:ems="10"
android:layout_weight="7"
/>
<Button
android:id="@+id/add_task_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/add_task_box"
android:layout_centerHorizontal="true"
android:layout_marginTop="-79dp"
android:onClick="addTask"
android:layout_weight="3"
android:text="New Task" />
</RelativeLayout>
Now add a class representing task. This simple class contains task
Listing 3: Task.java
public class Task {
private String mTask;
public Task(String task) {
this.mTask = task;
}
public String getTask() {
return mTask;
}
}
Listing 4: RecyclerViewAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
Listing 5: RecyclerViewHolders.java
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.List;
public class RecyclerViewHolders extends RecyclerView.ViewHolder{
private static final String TAG =
RecyclerViewHolders.class.getSimpleName();
public TextView taskTitle;
public ImageView deleteIcon;
private List<Task> taskObject;
String taskTitle =
taskObject.get(getAdapterPosition()).getTask();
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference();
Query dQuery =
ref.orderByChild("task").equalTo(taskTitle);
dQuery.addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dSnapshot:
dataSnapshot.getChildren()) {
dSnapshot.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
Log.e(TAG, "onCancelled",
databaseError.toException());
}
});
}
});
}
}
Finally, we will have the main activity that will connect to Firebase
database and display the list of tasks in the recyclerview.
When the Button is clicked, the text content of the EditText is sent to the
Firebase database through Push() and setValue() methods of the
DatabaseReference class.
Listing 6: MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG =
MainActivity.class.getSimpleName();
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter recyclerViewAdapter;
private EditText addTaskBox;
private DatabaseReference databaseReference;
private List<Task> allTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allTask = new ArrayList<Task>();
databaseReference =
FirebaseDatabase.getInstance().getReference();
addTaskBox = (EditText)findViewById(R.id.add_task_box);
recyclerView = (RecyclerView)findViewById(R.id.task_list);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
databaseReference.addChildEventListener(new
ChildEventListener() {
@Override