Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Firebase Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud. To create and add data to Firebase Firestore in an Android application, follow these steps:
google-services.json
file and place it in the app
directory of your Android project.In your project-level build.gradle
file:
buildscript { // ... dependencies { // ... classpath 'com.google.gms:google-services:4.x.x' // Check for the latest version } }
In your app-level build.gradle
file:
dependencies { // ... implementation 'com.google.firebase:firebase-firestore:24.x.x' // Check for the latest version } // Apply the Google services plugin at the bottom of the file apply plugin: 'com.google.gms.google-services'
Sync your Gradle files.
To add data to Firestore:
// Initialize Firestore val db = Firebase.firestore // Create a new user with a first and last name val user = hashMapOf( "first" to "John", "last" to "Doe", "born" to 1980 ) // Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener { documentReference -> Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}") } .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) }
In the example above:
HashMap
with some user data.users
collection.Remember that Firestore is NoSQL, so you don't need to predefine tables or structures. Collections and documents can be created on the fly.
Always ensure that your Firestore rules are set correctly, especially if you're using Firestore in a production app. The default rules may prevent reading/writing if you haven't configured them yet. Proper Firestore rules can help you prevent unauthorized access and secure user data.
With Firestore, you can efficiently build real-time databases for your apps, integrate with authentication, cloud functions, and more.
Adding data to Firestore in Android Kotlin:
// Example of adding data to Firestore val db = FirebaseFirestore.getInstance() val collectionReference = db.collection("users") val user = hashMapOf( "name" to "John Doe", "email" to "john@example.com" ) collectionReference.add(user) .addOnSuccessListener { documentReference -> // Data added successfully Log.d("Firestore", "Document added with ID: ${documentReference.id}") } .addOnFailureListener { e -> // Handle errors Log.e("Firestore", "Error adding document", e) }
Creating a Firestore collection in Android:
// Example of creating a Firestore collection val db = FirebaseFirestore.getInstance() val collectionReference = db.collection("books") // Add documents to the "books" collection
Firestore document creation in Android example:
// Example of creating a Firestore document val db = FirebaseFirestore.getInstance() val collectionReference = db.collection("cities") val city = hashMapOf( "name" to "New York", "population" to 8419600 ) collectionReference.add(city) .addOnSuccessListener { documentReference -> // Document added successfully Log.d("Firestore", "Document added with ID: ${documentReference.id}") } .addOnFailureListener { e -> // Handle errors Log.e("Firestore", "Error adding document", e) }
Handling Firestore transactions in Android:
// Example of a Firestore transaction val db = FirebaseFirestore.getInstance() val docRef = db.collection("cities").document("SF") db.runTransaction { transaction -> val snapshot = transaction.get(docRef) val newPopulation = snapshot.getLong("population")!! + 1 transaction.update(docRef, "population", newPopulation) } .addOnSuccessListener { Log.d("Firestore", "Transaction success!") } .addOnFailureListener { e -> // Handle errors Log.e("Firestore", "Transaction failure", e) }
Firebase Firestore real-time updates in Android:
// Example of real-time updates in Firestore val db = FirebaseFirestore.getInstance() val docRef = db.collection("cities").document("SF") docRef.addSnapshotListener { snapshot, e -> if (e != null) { // Handle errors Log.e("Firestore", "Listen failed", e) return@addSnapshotListener } if (snapshot != null && snapshot.exists()) { Log.d("Firestore", "Current data: ${snapshot.data}") } else { Log.d("Firestore", "Current data: null") } }