How to Connect an Android Application to Firebase Realtime Database / Firestore and write a simple database (Kotlin / Java)

I’ll be show you simple way, how your project connecting with firebase 😊

add project
my project firebase

The first way is using Android Studio 🙈

step connect account
choose firebase
connect with Firestore
connect with Realtime Database
click to connect
select you project
complete to connected firestore
complete to connected realtime database

WHAT NEXT …??

see database

HELLO WORLD!! Lets Write.

1. Simple Write “Hello World” on Firestore

val db = FirebaseFirestore.getInstance()

val user: MutableMap<String, Any> = HashMap()
user["word1"] = "Hello"
user["word2"] = "World"
user["word3"] = "Im rivaldy!! :)"

db.collection("hello")
.add(user)
.addOnSuccessListener { documentReference -> Log.d("Message", "DocumentSnapshot added with ID: " + documentReference.id) }
.addOnFailureListener { e -> Log.w("Message", "Error adding document", e) }
FirebaseFirestore db = FirebaseFirestore.getInstance();
Map<String, Object> user = new HashMap<>();
user.put("word1", "Hello");
user.put("word2", "World");
user.put("word3", "Im rivaldy!! :)");
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);
}
});
rules firestore database
hello world firestore

2. Simple Write “Hello World” on Realtime database

val database = FirebaseDatabase.getInstance()
val myRef = database.getReference("message")
myRef.setValue("Hello World!! Im Rival :)")
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(“message”);
myRef.setValue(“Hello World!! Im Rival :)”);
hello world!!

FINISH…. :) WHAT NEXT !??

Thankyou….. 😇😇

i write just for fun | find me on my github https://github.com/im-o | @rivaldy_o ☕🎧