
Introduction
In this article, we will learn how to use Firebase Cloud Fire Store in Android and how to do CRUD operations with Firebase Cloud Fire Store.
Getting started with Firebase
Read the following to learn how to set up the Firebase in your Android project.
Reference - https://androidmads.blogspot.in/2016/10/android-getting-started-with-firebase.html
Additionally, do the following for Cloud Firestore.
You can enable the test mode to make the database accessible to all users.

At this point, the empty DB is created as shown in the figure below.

Firebase Cloud Fire store
It is a flexible, scalable NoSQL cloud database to store and sync data for the client- and server-side development.
Key capabilities
- Flexibility
- Expressive Querying
- Real-time Updates
- Offline Support
- Designed to Scale
Concepts
Firestore hosted the data like NoSQL Database.
Database -> Collections
Table -> Document
Column -> Field
Coding Part
I have split this part into 3 steps as in the following.
- Step 1 - Creating a New Project with Empty Activity.
- Step 2 - Setting up the Firebase Library.
- Step 3 - Implementation of CRUD with Cloud Firestore.
Step 1 - Create a new project with Empty Activity
- Open Android Studio and Select create a new project.
- Name the project as per your wish and select an Empty activity.

- Click the “Finish” button to create a new project in Android Studio.
Step 2 - Setting up the Firebase Library
In this part, we will see how to set up the library for the project.
- Open your project level build.gradle file and add the following lines in dependencies
- {
- …
- classpath 'com.google.gms:google-services:3.1.0'
- …
- }
- Then add the following lines in all projects in the project level build.gradle file.
- allprojects {
- repositories {
- google()
- jcenter()
- maven {
- url "https://maven.google.com"
- }
- }
- }
- Then add the following lines in app level build.gradle file to apply Google services to your project.
- dependencies {
- ...
- implementation 'com.google.firebase:firebase-firestore:11.8.0'
- }
- Then click “Sync Now” to set up your project.
Step 3 - Implementation of CRUD with Cloud Firestore
In this part, we will see how to implement CRUD operations with Firestore.
First, we will initialize the Firestore Database.
- FirebaseFirestore myDB;
- // Init FireStore
- myDB = FirebaseFirestore.getInstance();
INSERT DATA
You can insert the data to Firestore like the following.
- Map<String, Object> data = new HashMap<>();
- data.put("task_name", edtData.getText().toString());
- myDB.collection("tasks")
- .add(data)
- .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
- @Override
- public void onSuccess(DocumentReference documentReference) {
- toastResult("Data added successfully");
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- toastResult("Error while adding the data : " + e.getMessage());
- }
- });


Thiago VivasPosted Apr 29, 2018, 12:04 PM
Good one, well explained.
Vignesh ManiPosted Apr 29, 2018, 2:09 AM
Nice One Thank you for Sharing