Introduction

This article demonstrates how to Retrieve Real-Time Data in Firebase using Android Studio. Also, you will see how to get the data from a Firebase real-time database. In this part, I will complete the remaining process of retrieving real-time data using Firebase.
Refer to part one from the following link.

Real-Time Database

Firebase provides a real-time database and back-end as a service. The service provides application developers an API that allows application data to be synchronized across clients and stored on Firebase's cloud. The company provides client libraries that enable integration with Android, iOS, JavaScript, Java, Objective-C, swift and Node.js applications. The database is also accessible through a REST API and bindings for several JavaScript frameworks such as AngularJS, React, Ember.js and Backbone.js. The REST API uses the Server-Sent Events protocol, which is an API for creating HTTP connections for receiving push notifications from a server.
Prerequisites
The steps given below are required to be followed in order to create a retrieve data in Android, using Android Studio.
Step 1
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' }
Gradle for app
  1. dependencies {
  2. implementation fileTree(dir: 'libs', include: ['*.jar'])
  3. implementation 'com.android.support:appcompat-v7:26.1.0'
  4. implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  5. testImplementation 'junit:junit:4.12'
  6. androidTestImplementation 'com.android.support.test:runner:1.0.1'
  7. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
  8. implementation 'com.google.firebase:firebase-core:15.0.0'
  9. implementation 'com.google.firebase:firebase-database:15.0.0'
  10. implementation 'com.firebase:firebase-client-android:2.4.0'
  11. }
  12. apply plugin: 'com.google.gms.google-services'
Step 2
Go to Console Firebase > Click OverView > Database > Real-Time DB. Select the Real-Time DB to create three new Name and Value, just copy the Database table link for Firebase DB.
Step 3
Next, go to app >> res >> layout >> activity_main.xml. Select the files to follow the code as given below.
  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:androidRelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Buttonandroid:idButtonandroid:id="@+id/button"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentTop="true"
  12. android:layout_centerHorizontal="true"
  13. android:layout_marginTop="155dp"
  14. android:text="Button"/>
  15. </RelativeLayout>
Step 4
Next, go to app >> src >> java >> Domain Name >> MainActivity.java . Select MainActivity file to follow the code as given below.
  1. package io.github.saravanan_selvaraju.myapplication;
  2. import android.support.annotation.MenuRes;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  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. import java.util.Map;
  13. publicclass MainActivity extends AppCompatActivity
  14. {
  15. private Firebase mRef;
  16. private Button mbutton;
  17. @Overrideprotectedvoid
  18. onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. mRef = new Firebase("https://retrieve-f165f.firebaseio.com"); // DataBase Profile Link
  23. mbutton = (Button) findViewById(R.id.button);
  24. mbutton.setOnClickListener(new android.view.View.OnClickListener()
  25. {
  26. @Overridepublicvoid onClick(android.view.View view)
  27. { mRef.addValueEventListener(new ValueEventListener()
  28. {
  29. @Overridepublicvoid onDataChange(DataSnapshot dataSnapshot)
  30. { Map<String, String>map = dataSnapshot.getValue(Map.class);
  31. String name = map.get("Name");
  32. String age = map.get("Age");
  33. String state = map.get("State");
  34. Log.v("E_VALUE","Name:" + name);
  35. Log.v("E_VALUE","Age:" + age);
  36. Log.v("E_VALUE","State:" + state);
  37. } @Overridepublicvoid onCancelled(FirebaseError firebaseError)
  38. { }
  39. });}}); } }
"mRef = new Firebase("....LINK.... ");".
This is your firebase DB link, It is used to help the database table connect to your app
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.
FIRST OUTPUT
When you click on a button, that data will be displayed in the given Logcat.
Step 5
Next, go to app >> res >> layout >> activity_main.xml. Select the files to follow the code as given below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ListView
  9. android:id="@+id/ListView"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:layout_alignParentTop="true" />
  13. </RelativeLayout>
Preview
Step 6
Again, go to app >> src >> java >> Domain Name >> MainActivity.java . Select MainActivity file to follow the code as given below.
  1. package io.github.saravanan_selvaraju.myapplication;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.ListView;
  6. import com.firebase.client.ChildEventListener;
  7. import com.firebase.client.DataSnapshot;
  8. import com.firebase.client.Firebase;
  9. import com.firebase.client.FirebaseError;
  10. import java.util.ArrayList;
  11. public class MainActivity extends AppCompatActivity {
  12. private Firebase mRef;
  13. private ArrayList<String> mUsername = new ArrayList<>();
  14. private ListView mListView;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. mRef = new Firebase("https://retrieve-f165f.firebaseio.com"); // DataBase Profile Link
  20. mListView = (ListView) findViewById(R.id.ListView);
  21. final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mUsername);
  22. mListView.setAdapter(arrayAdapter);
  23. mRef.addChildEventListener(new ChildEventListener() {
  24. @Override
  25. public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  26. String value = dataSnapshot.getValue(String.class);
  27. mUsername.add(value);
  28. arrayAdapter.notifyDataSetChanged();
  29. }
  30. @Override
  31. public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  32. }
  33. @Override
  34. public void onChildRemoved(DataSnapshot dataSnapshot) {
  35. }
  36. @Override
  37. public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  38. }
  39. @Override
  40. public void onCancelled(FirebaseError firebaseError) {
  41. }
  42. });
  43. }
  44. }
Step 7
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
Real-Time data will be displayed in the given ListView.

Summary

Now, you have successfully retrieved Real-Time Data in an Android application using Andriod Studio and Java.