0% found this document useful (0 votes)
8 views5 pages

Firebase MAD

This document is a tutorial for integrating Firebase into a basic Android app, covering setup, authentication rules, and data handling. It guides users through adding Firebase as a dependency, configuring network permissions, writing data to Firebase, and implementing event listeners for data retrieval. The tutorial concludes with encouragement to seek help if needed and provides links to download both the basic and completed app versions.

Uploaded by

insafian023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Firebase MAD

This document is a tutorial for integrating Firebase into a basic Android app, covering setup, authentication rules, and data handling. It guides users through adding Firebase as a dependency, configuring network permissions, writing data to Firebase, and implementing event listeners for data retrieval. The tutorial concludes with encouragement to seek help if needed and provides links to download both the basic and completed app versions.

Uploaded by

insafian023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSE 110 Discussion - Firebase Tutorial

October 12, 2016


Daniel Pan

1 Introduction
Today we’ll be going over Firebase which is a database backend. It provides various features such
as authentication and cloud data storage. We will build off a basic app and add firebase to it.
This is a very simple app that just has info on a class with students with names and ids.

Click Here to download the basic app that we’ll be adding Firebase to.
Also, Click Here to download the completed firebase app. If at any time you are stuck at a part,
you can refer to the completed app for help.

When you receive it, unzip the contents to your desktop.

2 Firebase
Now go to https://www.firebase.com/login/ and login a google account and create a project. Keep
track of your project ID. As you can see in the screenshot, mine is himitsuno-fecb4

We will now change our project’s authentication rules to public. On the left, click on Database >
Rules. You should be on this page. Here you can set who is allowed access to your database.

Let’s make it public. Change it to:

1
3 Adding Firebase to Basic App as a dependency
Now that we have finished setting up Firebase, open the basic app in Android Studio. If it tells you
to update or install build tools, say yes. First we will add Firebase as a project dependency. Now
navigate to BasicApp > app > build.gradle. Scroll to the bottom and you’ll see Dependencies.
Add to the bottom of dependencies this line
compile ’com.firebase:firebase-client-android:2.5.0+’
Also add some excludes to our Android packaging options

android {
packagingOptions {
exclude ’META-INF/LICENSE’
exclude ’META-INF/LICENSE-FIREBASE.txt’
exclude ’META-INF/NOTICE’
}
}
Your build.gradle file should now look like Click Here. Click sync. It is the circular arrows button
next to save.

4 Adding Network Permissions to BasicApp


Next we will Add network permissions to our Android app Navigate to BasicApp > app > src >
main > AndroidManifiest.xml After the 3rd line package add the lines
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Your AndroidManifest.xml file should now look like Click Here

5 Writing Data to Firebase in Basic App


Next we’ll setup firebase context in the application We need to initialize the firebase library.
Navigate to BasicApp > app > src > main > java > MainActivity. We will first import firebase:
import com.firebase.client.Firebase;
Under the onCreate function after the line super.onCreate(); Initialize firebase with
Firebase.setAndroidContext(this);

2
Now we create a reference to the Firebase database using the Firebase Project ID we saved earlier.
under public void submit we initialize the firebase constructor with your firebase url.
Firebase myFirebaseRef = new Firebase("https://<firebase-url>.firebaseio.com");
For my Firebase this would be:
Firebase myFirebaseRef = new Firebase("https://himitsuno-fecb4.firebaseio.com");
With this reference we can now write any data value to it. For example, to write the student’s Id
it will just be:
myFirebaseRef.child(student.getStudentId()).setValue(student);
As a reminder this is a very simple app that just has info on a class with students with names
and ids.
This is a good time to run our app to make sure nothing is wrong. By this time we should have
firebase connected and the ability to connect studentID to studentName. If we all did it correctly
it will sync and run with no problems. Click on Add Record and go ahead and add a student to
the database. In my example I added Himitsuno with student ID 0009. When you click on submit
this entry will be written to the Firebase database.

Your MainActivity file should now look like Click Here with your Firebase url instead of mine.

6 Event Listeners and Reading Data from Firebase in Ba-


sic App
Looks good. We have added the ability to add a student. Lastly we want the ability to search
for a student by ID. This involves reading data from Firebase. We will do this by attaching an
event listener and handling the event. Navigate to BasicApp > app > src > main > java >
SearchActivity. Add these 6 imports at the top.
import android.widget.Toast;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;

3
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
First initialize the reference with your Firebase ID like we did in MainActivity.
Firebase myFirebaseRef = new Firebase("https://<firebase-url>.firebaseio.com");
For my Firebase this would be:
Firebase myFirebaseRef = new Firebase("https://himitsuno-fecb4.firebaseio.com");
Remember to use your own Firebase ID! Now add this huge chunk of code at the bottom of public
void search
Firebase myFirebaseRef = new Firebase("https://himitsuno-fecb4.firebaseio.com");
Query queryRef = myFirebaseRef.orderByChild("studentId").equalTo(idToSearch);
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot == null || snapshot.getValue() == null)
Toast.makeText(SearchActivity.this, "No record found", Toast.LENGTH_SHORT).show();
else
Toast.makeText(SearchActivity.this, snapshot.getValue().toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(FirebaseError error) {
}
});
Essentially we attach an event listener, write a query reference, search the database once for our
query and then unregister the listener. In this case it is useful for a callback to be called once and
then immediately removed. That is exactly what addListenerForSingleValueEvent does.
Your SearchActivity file should now look like Click Here with your Firebase url instead of mine.
Now sync and run BasicApp again and we can now search for students in our database by ID.
For example, if I input ID 0009 and hit search it will return the student Himitsuno I added earlier.

4
Once you have made it this far, congragulations. You have completed the Firebase tutorial. If
you are stuck on any point please refer to the completed Firebase app and see where you went
wrong. If there are any further questions you are welcome to come to our Office Hours!

You might also like