How to Retrieve Data from the Firebase Realtime Database in Android?
Firebase Realtime Database is the backend service which is provided by Google for handling backend tasks for your Android apps, IOS apps as well as your websites. It provides so many services such as storage, database, and many more. The feature for which Firebase is famous is for its Firebase Realtime Database. By using Firebase Realtime Database in your app you can give live data updates to your users without actually refreshing your app. So in this article, we will be creating a simple app in which we will be using Firebase Realtime Database and retrieve the data from Firebase Realtime database and will see the Realtime data changes in our app.
What we are going to build in this article?
We will be building a simple Android application in which we will be displaying a simple Text View. Inside that TextView, we will be displaying data from Firebase. We will see how Real-time data changes happen in our app by actually changing data on the server-side. Note that we are going to implement this project using both Java and Kotlin language.
Note: You may also refer to How to Save Data to the Firebase Realtime Database in Android?
Step by Step Implementation
Step 1: Create a new project and Connect to Firebase app
Refer to Adding firebase to android app and follow the steps to connect firebase to your project.
Step 2: Add Realtime Database to you app in Console
– Go to Firebase console and navigate to your project, and on the left side of the screen, under Build choose Realtime Database. On the next screen, select Create Database.
– On the dialog box that appears, choose a Realtime Database Location and click on Next. Then select Start in locked mode and click on Enable. You can’t change the Realtime Database Location later, so be careful while choosing.

Now, in Realtime Database, under Rules make the following changes:

Step 3: Add Realtime Database to you app in Android Studio
Navigate to Tools > Firebase. This will open the Firebase Assistant tab. Now select Realtime Database > Get started with Realtime Database.

Now select Add the Realtime Database SDK to your app, then select Accept Changes in the dialog box. This will add all the necessary dependencies for realtime database.

Step 4: Add necessary permissions
For adding data to Firebase we should have to give permissions for accessing the internet. For adding these permissions navigate to the app > AndroidManifest.xml and inside that file add the below permissions to it.
<uses-permission android:name="android.permission.INTERNET"/>
Step 5: Create a model class for User Info
Right click on app and select New > Kotlin/Java class file and provide the name Person to the file. Add the following code to the file.
package org.geeksforgeeks.demo;
public class Person {
private String name;
private String number;
private String address;
public Person() {}
public Person(String name, String number,
String address)
{
this.name = name;
this.number = number;
this.address = address;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getNumber() { return number; }
public void setNumber(String number)
{
this.number = number;
}
public String getAddress() { return address; }
public void setAddress(String address)
{
this.address = address;
}
}
package org.geeksforgeeks.demo
class Person (
var name: String? = null,
var number: String? = null,
var address: String? = null
)
Step 6: Working with MainActivity and it’s layout file
In the previous article, How to Save Data to the Firebase Realtime Database in Android?, we learned how to save data in realtime database. In this article, we will retrieve the same data that we saved in the previous article. Refer to the image below to check the data we saved.

Navigate to app > java > package name > MainActivity and app > res > layout > activity_main.xml. Then add the following code to the MainActivity file in Java or Koltin and the xml code to the activity_main.xml file.
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.*;
public class MainActivity extends AppCompatActivity {
private TextView nameTextView, phoneTextView, addressTextView;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameTextView = findViewById(R.id.nameTextView);
phoneTextView = findViewById(R.id.phoneTextView);
addressTextView = findViewById(R.id.addressTextView);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("PersonData");
retrieveFirstDataFromFirebase();
}
private void retrieveFirstDataFromFirebase() {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Person person = dataSnapshot.getValue(Person.class);
if (person != null) {
nameTextView.setText("Name: " + person.getName());
phoneTextView.setText("Phone: " + person.getNumber());
addressTextView.setText("Address: " + person.getAddress());
break;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this, "Failed to retrieve data: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.database.*
class MainActivity : AppCompatActivity() {
private lateinit var nameTextView: TextView
private lateinit var phoneTextView: TextView
private lateinit var addressTextView: TextView
private lateinit var firebaseDatabase: FirebaseDatabase
private lateinit var databaseReference: DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nameTextView = findViewById(R.id.nameTextView)
phoneTextView = findViewById(R.id.phoneTextView)
addressTextView = findViewById(R.id.addressTextView)
firebaseDatabase = FirebaseDatabase.getInstance()
databaseReference = firebaseDatabase.getReference("PersonData")
retrieveFirstDataFromFirebase()
}
private fun retrieveFirstDataFromFirebase() {
databaseReference.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (dataSnapshot in snapshot.children) {
val person = dataSnapshot.getValue(Person::class.java)
person?.let {
nameTextView.text = "Name: ${it.name}"
phoneTextView.text = "Phone: ${it.number}"
addressTextView.text = "Address: ${it.address}"
}
break
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(this@MainActivity, "Failed to retrieve data: ${error.message}", Toast.LENGTH_SHORT).show()
}
})
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:padding="32dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrieved Data"
android:textSize="18sp"
android:textStyle="bold"
android:paddingBottom="10dp"/>
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "
android:textSize="16sp"/>
<TextView
android:id="@+id/phoneTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone: "
android:textSize="16sp"/>
<TextView
android:id="@+id/addressTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: "
android:textSize="16sp"/>
</LinearLayout>
Output:
