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 Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in real-time. Here's a basic guide on integrating Firebase Realtime Database in an Android application with some CRUD operations as examples:
Add the following dependencies to your app's build.gradle
:
implementation 'com.google.firebase:firebase-core:19.0.0' implementation 'com.google.firebase:firebase-database:19.7.0'
In your main activity or application class:
FirebaseApp.initializeApp(this);
DatabaseReference database = FirebaseDatabase.getInstance().getReference(); // Push data to a "users" child node DatabaseReference userReference = database.child("users").push(); userReference.setValue(new User("John", "john@example.com"));
Assuming User
is a simple class:
public class User { public String name; public String email; public User() {} public User(String name, String email) { this.name = name; this.email = email; } }
To read data once:
database.child("users").addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { User user = childSnapshot.getValue(User.class); // Do something with the user } } @Override public void onCancelled(DatabaseError error) { // Handle errors here } });
To listen for real-time changes:
database.child("users").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { User user = childSnapshot.getValue(User.class); // Do something with the user } } @Override public void onCancelled(DatabaseError error) { // Handle errors here } });
DatabaseReference userToUpdate = database.child("users").child("user_id"); userToUpdate.child("name").setValue("Updated Name");
Replace "user_id"
with the actual ID of the user to be updated.
DatabaseReference userToDelete = database.child("users").child("user_id"); userToDelete.removeValue();
Replace "user_id"
with the actual ID of the user to be deleted.
Make sure to set up your database rules appropriately. By default, the database might either not be readable/writable by anyone or be open to everyone, both of which are not ideal for production apps.
In the Firebase console, navigate to Realtime Database > Rules and set up rules that fit your app's needs. For example, to allow authenticated users to read/write:
{ "rules": { ".read": "auth != null", ".write": "auth != null" } }
This is a basic introduction, and there's a lot more that Firebase Realtime Database offers, such as complex querying, offline support, and more. Always refer to the official Firebase documentation for detailed information and best practices.
CRUD operations with Firebase Realtime Database example code:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("exampleData"); // Create operation String key = databaseReference.push().getKey(); databaseReference.child(key).setValue("Data to be stored"); // Read operation databaseReference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // Read data from dataSnapshot } @Override public void onCancelled(DatabaseError error) { // Handle read error } }); // Update operation databaseReference.child(key).setValue("Updated data"); // Delete operation databaseReference.child(key).removeValue();
Handling offline data sync with Firebase Realtime Database:
// Enable offline persistence FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Firebase Realtime Database queries and filtering in Android:
// Query example to filter data Query query = databaseReference.orderByChild("age").equalTo(25);
Securing Firebase Realtime Database with authentication:
// Check if the user is authenticated FirebaseAuth auth = FirebaseAuth.getInstance(); if (auth.getCurrentUser() != null) { // User is authenticated, access the database }