Retrieve Real-Time Data In Firebase Using Android Studio


This article demonstrates how to Retrieve Real-Time Data in Firebase using Android Studio. Also, you will see how to get the new Firebase data to your real-time database.

Retrieve Real-Time Data

Obtaining data from a database management system: In this case, it is considered that data is represented in a structured way, and there is no ambiguity in data. In order to retrieve the desired data, the user presents a set of criteria using a query. Data retrieval is the process of identifying and extracting data from a database, based on a query provided by the user or application. It enables the fetching of data from a database in order to display it on a monitor and/or use within an application.
Step 1
Create a new project in Android Studio from File >> Project and fill in all the necessary details.
Step 2
Create a new project in Firebase console ""
Enter your application package name. The Nick Name and Debug certificate SHA-1 are optional.
You need to download a google-services.json file and just move the downloaded google-services file into the Android app. Click on Database in your Project.
Step 3
Next, go to Gradle Scripts >> build.gradle (Module: app). Select build.gradle. The app Gradle compiles the code, and then the build types will appear. Just replace that with the following code and add the Firebase Core and Firebase Database Gradle to your project, or you can also use Gradle.
Dependencies Class
  1. dependencies {  
  2.         classpath ''  
  3.         classpath ''  
  4.     }  
Gradle for app
  1. dependencies {  
  3.     implementation ''  
  4.     implementation ''  
  5.     implementation 'com.firebase:firebase-client-android:2.4.0'  
  6. }  
  7. apply plugin: ''  
"implementation 'com.firebase:firebase-client-android:2.4.0" this gradle is used for client retrieve the data from DB.
Step 4
Next, go to app >> res >> layout >> activity_main.xml. Select the files to follow the snapshot as given below.
Step 5
Next, go to app >> Java >> package name >> New Java Class. Select the Java Class. Just use the following code. The file name is Code
  1. package io.github.saravanan_selvaraju.myapplication;    
  3. import;    
  4. import com.firebase.client.Firebase;    
  5. import;    
  7. public class FireApp extends Application {    
  9.     @Override    
  10.     public void onCreate() {    
  11.         super.onCreate();    
  12.         Firebase.setAndroidContext(this);    
  13.     }    
  14. }   
Step 6
Next, go to app >> manifest >> AndroidManifest.xml. Select the Manifest file to follow the snapshot. Just enable the INTERNET and enter the app name of ".FireApp".
Step 7
After that, again, go back to Console Firebase > Click OverView > Database > Real-Time DB. Select the Real-Time DB just copy the Database table link for Firebase DB.
Step 8
Next, go to app >> src >> java >> Domain Name >> . Select MainActivity  file to follow the code as given below. Code 
  1. package io.github.saravanan_selvaraju.myapplication;  
  3. import;  
  4. import;  
  5. import android.os.Bundle;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8. import com.firebase.client.DataSnapshot;  
  9. import com.firebase.client.Firebase;  
  10. import com.firebase.client.FirebaseError;  
  11. import com.firebase.client.ValueEventListener;  
  13. public class MainActivity extends AppCompatActivity {  
  15.     private TextView mtextView;  
  16.     private Firebase mRef;  
  17.     private Button mbutton;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  23.         mtextView = (TextView) findViewById(;  
  24.         mRef = new Firebase("..........................LINK..............................");  // DataBase Profile Link  
  25.         mbutton = (Button) findViewById(;  
  26.         mbutton.setOnClickListener(new android.view.View.OnClickListener() {  
  27.              @Override  
  28.             public void onClick(android.view.View view) {  
  30.                 mRef.addValueEventListener(new ValueEventListener() {  
  31.                     @Override  
  32.                     public void onDataChange(DataSnapshot dataSnapshot) {  
  33.                         String v = dataSnapshot.getValue(String.class);  
  34.                         mtextView.setText(v);  
  35.                     }  
  36.                     @Override  
  37.                     public void onCancelled(FirebaseError firebaseError) {  
  39.                     }  
  40.                 });}});  
  41. }  
  42. }  
"mRef = new Firebase("....LINK.... ");" .
This is your firebase DB link, It is used to help the database table connect to your app.
Step 9
Next, go to Android Studio and deploy the application. Select Emulator or your Android Device with USB debugging enabled. Give it a few seconds to make installations and set permissions.
When you click on a button, that data will be displayed in the given label.
We have successfully created Retrieved Real-time Data in Firebase app.
Refer to my previous article Real-Time Data Storage In Firebase Using Android Studio to get started with the basics of Firebase.