In: Computer Science
How to Connect an Android Application to Firebase Realtime Database and write a simple database in Java. Please attach screenshots of the activity performed.
Steps to connect to Firebase / Realtime database:
I am using android studio to create an android application ,
there are many other ways.
1. First go to https://console.firebase.google.com/ (firebase /
google console) to make a project . As the page loads , in the body
section below the header Create Project option is shown if you are
new to firebase , otherwise Add Project button is shown as in the
screenshot.(Please refer screenshot Capture1)
Capture1.png
Click on the button and proceed a. enter your project name and press continue b. Please check any terms and conditions checkboxes and continue until done . Please refer screenshot for the same. (Please refer screenshot Capture2, Capture3)
Capture2.png
Capture3.png
2. Create a new firebase database by clicking on Cloud Firestore
as shown in the screenshot(Please refer screenshot Capture5) and
then on create database that comes in the next page.(Please refer
screenshot). (Please refer screenshot Capture6)
Create database> next >(until done).
Capture5.png
Capture 6.png
3.Now go to your android studio and connect to your account, the same one as that used in firebase console. After login, on the topmost right side you will have a icon which shows that you have added an account.
4. In Android Studio in the topmost toolbar go to Tools>
Firebase (Please refer screenshot Capture8)
Capture8.png
5. A window is shown that gives the settings of firebase. Here you get all the options such as In-App Messaging, Invites , etc; basically whatever you want to do with te database. Here we select firestore and Realtime Database. (Please refer screenshot Capture9 , Capture10) .
Capture9.png
Capture10.png
6. Click on Read and Write Document to Cloud Firestore and add the dependencies as shown in screenshot(Capture11) . Click on connect to firebase> Select the project that you have created in (1a) > Click on Connect to Firebase . After this the console goes back to Capture11 , now click on Add Cloud Firestore to your App > click Accept Changes as shown in figure Capture12. Now repeat the same for Realtime Database by selecting Save and retrieve data from capture10.
Capture11.png
Capture12.png
7. If you see your project like bwlow(Capture13 , Capture14) then click finish
Capture13.png
Capture14.png
8. Now lets go to firebase console , you should get something
like below.
Here completed the setup . Now lets write a simple database java
program .
1. Firestore database : First lets the change the rules by clicking
on database to enable it to read and write documents to database.
Please find below screenshot (Capture16)
Capture16.png
Java program:
FirebaseFirestore db = FirebaseFirestore.getInstance(); //retrieve
an instance of our database
Map<String, Object> user = new HashMap<>(); // create a
user map to store the field names and values
user.put("word1", "[email protected]"); // sample mail is given
user.put("word2", "XYZ");
//Adding a new document with generated ID
db.collection("hello")
.add(user)
.addOnSuccessListener(new
OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "DocumentSnapshot added with ID: " +
documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
}
});
After this it writes something like below: