Question

In: Computer Science

How to Connect an Android Application to Firebase Realtime Database and write a simple database in...

How to Connect an Android Application to Firebase Realtime Database and write a simple database in Java. Please attach screenshots of the activity performed.

Solutions

Expert Solution

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:


Related Solutions

the code base you will be working with involves an Android application. (Android Studio) application name...
the code base you will be working with involves an Android application. (Android Studio) application name " SharingApp" In the application’s current state: A user of the app is able to create and edit a profile with a unique username and an email address. A user of the app is able to login and logout. An owner is able to record the items they own and wish to share. A bidder is able to place bids on items they wish...
Write a simple code for a simple connect the dots game in plain C coding.
Write a simple code for a simple connect the dots game in plain C coding.
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Write a code in order to make an android application of LZ77 compression algorithm for text...
Write a code in order to make an android application of LZ77 compression algorithm for text compression.
You are asked to design a relational database for a simple course registration software application for...
You are asked to design a relational database for a simple course registration software application for your school. The relational database must have the following information about the student, the course, and the registration, respectively StudentID, FirstName, LastName, DataOfJoining, and Major CourseNumber, CourseName,InstructorName, StartDate, EndDate, NumberOfCredits ReferenceID, StudentID,CourseID, DateOfRegistration Apply the following constrains while designing the database Each student in the database must be uniquely identifiable Each course listed in the database must be have unique CourseNumber Each course registration...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used by the customers to book cinema tickets. Only users of 15 years old and above are allowed to book cinema tickets for a particular film. The user can book more than one ticket where ticket prices vary between 20 and 50 AED. Your app contains tree Activities. The first activity is a launching activity containing a logo and a button start. When the user...
Using PHP and MYSQL and with a simple customer database, how can I create a simple...
Using PHP and MYSQL and with a simple customer database, how can I create a simple log in and registration system for an ecommerce site
XI. Application of These Theories Connect face recognition theory to how this explains an educator's own...
XI. Application of These Theories Connect face recognition theory to how this explains an educator's own biases. Provide strategies or ways for educators to check their perceptions and face recognition biases when relating to students and parents, as well as when recalling information about students and a situation.
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple HR application using the C++ programming language and Oracle server. This assignment helps students learn a basic understanding of application development using C++ programming and an Oracle database Submission: This Milestone is a new project that simply uses what was learned in the SETUP. Your submission will be a single text-based .cpp file including your C++ program for the Database Application project/assignment. The file...
How can we write an application that calculates connascence of an application?
How can we write an application that calculates connascence of an application?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT