Firebase is a famous product of Google which is used by so many developers to add backend functionality for their website as well as apps. The Firebase will make your job really easier for backend database and handling the database. In this article, we will take a look at the implementation of Firebase Firestore in Android. This is a series of 4 articles in which we are going to perform the basic CRUD (Create, Read, Update, and Delete) operation with Firebase Firestore in Android. We are going to cover the following 4 articles in this series:
- How to Create and Add Data to Firebase Firestore in Android?
- How to Read Data from Firebase Firestore in Android?
- How to Update Data in Firebase Firestore in Android?
- How to Delete Data from Firebase Firestore in Android?
What is Firebase Firestore?
Firebase Firestore is a cloud NoSQL database that is used to add, retrieve, and update data inside your application. Basically, it is a database that is used to store data inside your Firebase console. The data in the Firestore is stored in the form of documents, so it becomes easy to manage this data inside Firebase.
What is the benefit of using Firestore over Firebase Realtime Database?
Firestore is having a better query than that of Firebase Realtime Database. It is highly scalable and data can be managed properly inside Firebase Firestore. It becomes easy to access data on the server-side inside the Firebase Firestore database. Firebase Firestore is much easier in comparison to Firebase Realtime Database. You can check a more detailed guide on Firebase Realtime Database vs Firestore database.
What we will be building in this article?
In this article, we will be building a simple Android Application in which we will be collecting the data from users via some TextFields and we will send that data to our Firebase Cloud Firestore console and that data will get store in our Firebase database.
Step by Step Implementation
Step 1: Create a new Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project. Navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.
Inside that column Navigate to Firebase Cloud Firestore. Click on that option and you will get to see two options on Connect app to Firebase and Add Cloud Firestore to your app. Click on Connect now option and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase. After connecting your app to Firebase you will get to see the below screen.
After that verify that dependency for Firebase Firestore database has been added to our Gradle file. Navigate to the app > Gradle Scripts inside that file. Check whether the below dependency is added or not. If the below dependency is not present in your build.gradle file. Add the below dependency in the dependencies section.
After adding this dependency sync your project and now we are ready for creating our app. If you want to know more about connecting your app to Firebase. Refer to this article to get in detail about How to add Firebase to Android App.
Step 3: Working with the AndroidManifest.xml file
For adding data to Firebase we should have to give permissions for accessing the internet. For adding these permissions navigate to the app > AndroidManifest.xml. Inside that file add the below permissions to it.
Step 4: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Step 5: Create a new Java class for storing the data
For sending data to the Firebase Firestore database we have to create an Object class and send that whole object class to Firebase. For creating an object class navigate to the app > java > your app’s package name > Right-click on it and click on New > Java Class > Give a name to your class. Here we named it Courses and add the below code to it.
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
After adding this code go to this link to open Firebase. After clicking on this link you will get to see the below page and on this page Click on Go to Console option in the top right corner.
After clicking on this screen you will get to see the below screen with your all project inside that select your project.
Inside that screen click n Firebase Firestore Database in the left window.
After clicking on the Create Database option you will get to see the below screen.
Inside this screen, we have to select the Start in test mode option. We are using test mode because we are not setting authentication inside our app. So we are selecting Start in test mode. After selecting on test mode click on the Next option and you will get to see the below screen.
Inside this screen, we just have to click on the Enable button to enable our Firebase Firestore database. After completing this process we just have to run our application and add data inside our app and click on the submit button. You will get to see the data added inside the Firebase Console.
Below is the output of the app.