Retrieve Real-Time Data In Firebase Using Android Studio

Introduction

 
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.
 
Preview 
 
 
 
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 "console.firebase.google.com"
 
 
 
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 'com.android.tools.build:gradle:3.1.1'  
  3.         classpath 'com.google.gms:google-services:3.2.0'  
  4.     }  
Gradle for app
  1. dependencies {  
  2.   
  3.     implementation 'com.google.firebase:firebase-core:15.0.0'  
  4.     implementation 'com.google.firebase:firebase-database:15.0.0'  
  5.     implementation 'com.firebase:firebase-client-android:2.4.0'  
  6. }  
  7. apply plugin: 'com.google.gms.google-services'  
"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 FireApp.java.
 
 
 
FireApp.java Code
  1. package io.github.saravanan_selvaraju.myapplication;    
  2.         
  3. import android.app.Application;    
  4. import com.firebase.client.Firebase;    
  5. import com.google.firebase.analytics.FirebaseAnalytics;    
  6.         
  7. public class FireApp extends Application {    
  8.         
  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 >> MainActivity.java . Select MainActivity  file to follow the code as given below.
 
 
MainActivity.java Code 
  1. package io.github.saravanan_selvaraju.myapplication;  
  2.   
  3. import android.support.annotation.MenuRes;  
  4. import android.support.v7.app.AppCompatActivity;  
  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;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.   
  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);  
  22.   
  23.         mtextView = (TextView) findViewById(R.id.textView);  
  24.         mRef = new Firebase("..........................LINK..............................");  // DataBase Profile Link  
  25.         mbutton = (Button) findViewById(R.id.button);  
  26.         mbutton.setOnClickListener(new android.view.View.OnClickListener() {  
  27.              @Override  
  28.             public void onClick(android.view.View view) {  
  29.   
  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) {  
  38.   
  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.
 
Output
 
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.


Similar Articles