Hey cod3re’s today in this example we will learn about firebase cloud firestore
What is Firebase.
firebase is a set of tools for development by google and offers variety of other feature to improve performance and speed as well as some other essential tools for building a production level app.
Firebase has 3 different plan’s and one of them is free with some basic features.
Link’s
subscribe=>
So without wasting your time let’s get straight into it..
Tutorial level
To Add Firebase into your project you need to connect firebase assistant to your project for that you need to to go into tool>firebase open assistant and find firestore after select firebase cloud firestore click into connect your app option and then follow along after adding all the necessary implication we will add two edittext into our Main Activity.xml
make sure to set the layout to linearlayout then add a button into .Xml assign all widgets to there corresponding id’s
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/name_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:inputType="text"
/>
<EditText
android:id="@+id/pass_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="number"
/>
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save DAta"
android:onClick="saveNote"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
after completing our mainactivity.xml we will jump into our MainActivity.java file.
MainActivity.java
package com.anon3mity.firebaseexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String KEY_NAME ="name";
private static final String KEY_PASSWORD ="pass";
private EditText editTextname;
private EditText editTextpass;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextname =(EditText)findViewById(R.id.name_editText);
editTextpass =(EditText)findViewById(R.id.pass_editText);
}
public void saveNote(View v){
String name = editTextname.getText().toString();
String pass = editTextpass.getText().toString();
Map<String, Object> note = new HashMap<>();
note.put(KEY_NAME, name);
note.put(KEY_PASSWORD, pass);
db.collection("Notebook").document("My First Note").set(note)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this,"success data passed",Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this,"error!",Toast.LENGTH_LONG).show();
Log.d(TAG,e.toString());
}
});
}
}
Firebase save our data into a form of collection and each collection’s contain other collection’s and we can pass our data in firebase throw android studio using the firebase auth class and firebase accept data in a tag and value format so for that we have to create HashMap<> for our data as you can see in above code.
that’s all for today cod3r’s i hope you understand how to use firebase firestore. and if you have any query about this example make sure to hit a comment..


