Menyambungkan Aplikasi Android ke Firebase Realtime Database / Firestore dan write database sederhana (Kotlin/Java)

Rivaldy
5 min readJan 29, 2020

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

Ok, before connecting firebase with your project, let’s create project name your first firebase project.

MAAF BELUM DI TRANSLATE, NANTI LAH, KALAU ADA WAKTU — LAGI BELAJAR INGGRIS COZ :v

Goto your firebase console or google console to make a project https://console.firebase.google.com and firebase workspace will be show. and klik +Add Project, after that just write your project name, in here i use project name Simple App Firestore, and wait until finish build project.

add project

and this is my project

my project firebase

Ok, it’s time!! let me show you simple way to connecting your project with firebase.

The first way is using Android Studio 🙈

many ways, but i’m only show your simple way

  • Connecting your Android studio with account gmail (same account your firebase). and on right side click account (here i have account before), after that klick add account and u’ll be redirect to browser and login with your gmail. (if your complete login you’ll be see your account on right side)
step connect account
  • And than, go to your project Android Studio and choose Tools . Firebase.
choose firebase
  • And a window will be show your setting your Firebase (here i choose firestore, and you can change with Realtime Database too)
connect with Firestore
connect with Realtime Database
  • Select Read and write…. or if you want touse Realtime Database select Save and retrieved and add dependencies firestore / realtime database on your project, and waiting for complete.
click to connect
  • 1. select Connect to firebase -> after your click Connect to firebase select your project, here i create Simple App Firestore before (because this i complete before, will be show 1 Android… if not will be empty. and click Connect to firebase. wait until finish.
select you project
  • 2. select Add Cloud Firestore to your app or in Realtime Database select Add the realtime database to your app and then click Accept Changes. wait until sycn finish.
  • Finish, if you see your project like bellow, you have successfully connected Firebase with your project 🙈
complete to connected firestore
complete to connected realtime database

WHAT NEXT …??

OK. let’s create simple write from your project to database firestore and realtime database

To see database Firestore and Realtime database, go to your firebase project and select Database.

see database

HELLO WORLD!! Lets Write.

1. Simple Write “Hello World” on Firestore

  • Open your android project and add this code in your mainActivity or other activity.

#Kolin

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) }

#Java

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);
}
});
  • dont forget to change rules firestore database to accept read and write, like this.
rules firestore database
  • Run your project and taraaa… you complete simple write.
hello world firestore

2. Simple Write “Hello World” on Realtime database

  • Open your android project and add this code in your mainActivity or other activity.

#Kotlin

val database = FirebaseDatabase.getInstance()
val myRef = database.getReference("message")
myRef.setValue("Hello World!! Im Rival :)")

#Java

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(“message”);
myRef.setValue(“Hello World!! Im Rival :)”);
  • dont forget to change rules realtime database to accept read and write, like this.
  • Run your program . and taraa… simple write is created. :)
hello world!!

FINISH…. :) WHAT NEXT !??

This is my simple CRUD Firestore, 😊

and find me on simple my github 😅😅🙈

Thankyou….. 😇😇

--

--

Rivaldy

Make it fun | Find me on my GitHub page at https://github.com/im-o. And let's connect over coffee! ☕ You can reach me on any social media at @rivaldy_o."