Getting Started with the Firebase Realtime Database on Android

First we learn how to get setup with the Realtime Database and then we go over the basics of the Realtime Database by building an app.

After that you’ll know everything you need to know to get started in just a few minutes!

  • Before we start you need to visit those Posts (MUST) :
  1. Add Firebase Dependency To Android Application With Android Studio
  2. Getting Started With Firebase
  3. Connect New Firebase Project To Android Application
  4. Understanding JSON Schema for Beginners

Set up Firebase Realtime Database for Android

  • Add Firebase Realtime Database SDK to Gradle File :
  • implementation ‘com.google.firebase:firebase-database:16.0.1’

  • Add Internet Permission :
  • <uses-permission android:name=”android.permission.INTERNET” />

  • You can add data to realtime database (Object by Object) or create json file, i prefer json file you can use this website to edit and create json files.
  • Create Data and Save as File.json :

  •  Click on Database :

  • Scroll-down and click on Create database :



  • Select ” Start in test mode ” and click Enable :

  • Nice, Now You have your own Realtime Database.
  • Lets Import the Json file by clicking on the three dots :

  • Click Import Json :

  • Hit Browse :

  • Select the json file and click open then Import :

  • Great ! Now Your Data is loaded :

  • Go Back To Android Studio.
  • In our Realtime Database we have 3 Objects “Strings” :
  1. Name
  2. Description
  3. Website
  • We need 3 TextView to Display our data to users :

Code :

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/andruni.name"
android:text="Name"
android:layout_centerHorizontal="true"
android:layout_above="@id/andruni.Description"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/andruni.Description"
android:layout_margin="20dp"
android:layout_centerInParent="true"
android:text="Description"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/andruni.Website"
android:layout_below="@id/andruni.Description"
android:text="Website"/>
  • After that Go to your MainActivity.java and paste or modified this code :

Code :

public class AndruniActivity extends AppCompatActivity {
DatabaseReference myRef;
FirebaseDatabase myDB;
TextView andruniName;
TextView andruniDescription;
TextView andruniWebstire;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_andruni);
andruniName = findViewById(R.id.andruni_name);
andruniDescription = findViewById(R.id.andruni_Description);
andruniWebstire = findViewById(R.id.andruni_Website);
myDB = FirebaseDatabase.getInstance();
myRef = myDB.getReference("andruni");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Data From Realtime Database
andruniName.setText(String.valueOf(dataSnapshot.child("Name").getValue()));
andruniDescription.setText(String.valueOf(dataSnapshot.child("Description").getValue()));
andruniWebstire.setText(String.valueOf(dataSnapshot.child("Website").getValue()));
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//Handle Errors
}});}}
  • Start The project.
  • as you can see the app is working :

firebase realtime database

Fore More Informations :

I hope that’s Clear & Simple.

Next Time We will learn how to Create And use Firebase Firestore Database.

Until thenhave a good time.

Android Dev

Android Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *