firebase-database Firebase Real-Time Database with Android Integrate Firebase Real-Time database with an Android application

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

How to implement Firebase Real-Time database in Android applications.

Setup/Installation:

  1. First, install the Firebase SDK (guide)

  2. Register your project using the Firebase console

  3. After successfuly completing the two steps above, add the following dependency in your application level gradel.

    compile 'com.google.firebase:firebase-database:9.2.1'
    
  4. [Optional] Configure your database security rules (reference).

Implementation Sample:

  1. Declare and initialize the database reference

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");
    

You can later create different references to access different nodes

  1. Write new data to the database

    myRef.setValue("Writing Demo");
    
  2. Read data from the database

     myRef.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
         // This method is called once with the initial value and again
         // whenever data at this location is updated.
         String value = dataSnapshot.getValue(String.class);
         Log.d(TAG, "Value is: " + value);
     }
    
     @Override
     public void onCancelled(DatabaseError error) {
         // Failed to read value
         Log.w(TAG, "Failed to read value.", error.toException());
     }
    });
    


Got any firebase-database Question?