Intermediate 30 min Java Full project

RecyclerView with Firebase Realtime Database (Java)

Load data from Firebase Realtime Database and show it in a RecyclerView. The list updates automatically when the data in Firebase changes.

What you'll learn

  • Connect your app to Firebase
  • Read data from Realtime Database
  • Map it into model objects
  • Show it in a RecyclerView that updates live

Before you start

  • Android Studio (any recent version)
  • Basic Java: variables, methods and if/else
  • A free Firebase project (console.firebase.google.com)
  • An emulator or an Android phone

How it fits together

  1. 1Add form
  2. 2users.push()
  3. 3Realtime Database
  4. 4ValueEventListener
  5. 5ListAdapter cards

Build it step by step

1. Create the database

In the Firebase console, add an Android app with your package name (com.example.recyclerviewfirebase to copy the code unchanged) and download google-services.json into the app folder. Then open Realtime Database → Create database and choose test mode while you learn.

2. Add Firebase to the app

In app/build.gradle.kts, add the plugin and dependencies:

kotlin
plugins {
    id("com.google.gms.google-services")
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
    implementation("com.google.firebase:firebase-database")
}

In the project-level build.gradle.kts, add:

kotlin
plugins {
    id("com.google.gms.google-services") version "4.5.0" apply false
}

Then press Sync Now.

3. Describe one user

User has public name, email and age fields and an empty constructor. Firebase uses them to turn database data into objects and back. The key field is marked @Exclude, so it isn't saved.

4. Add users

users.push().setValue(user) saves a user under a new unique key in the users node. You can also add users by hand in the console.

5. Listen for changes

Add a ValueEventListener to users in onStart. Whenever anything changes, onDataChange receives every user. Turn each child into a User, remember its key, and build a list.

6. Show the list

MyAdapter is a ListAdapter. Call submitList(list) and DiffUtil works out what changed, so only those cards animate.

7. Delete

The delete icon calls users.child(user.key).removeValue(). The listener then sends the new list automatically.

8. Stop listening

Remove the listener in onStop, so the app doesn't use data in the background.

9. Run it

Add a user, then open the console and edit or add one there. Watch the app update instantly; open it on two phones to see them stay in sync.

If something goes wrong

  • "Could not read the database": check the database Rules. Test mode allows reads and writes for 30 days.
  • Fields are empty: the names in User must match the names in the database exactly.
  • Before you publish: replace test-mode rules with rules that require sign-in.

What's improved over the video

You can add and delete users from the app, cards animate smoothly with DiffUtil, there's an empty-state message, and the listener stops when the screen is hidden.

Required setup

These are additions to an existing project—not replacement project files.

Add Firebase Realtime Database

Add these lines

Where: app/build.gradle.kts and build.gradle.kts

Add the plugin and dependencies, put google-services.json in the app folder, then Sync Now.

app/build.gradle.kts and build.gradle.kts
// app/build.gradle.kts
plugins {
    id("com.google.gms.google-services")
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
    implementation("com.google.firebase:firebase-database")
}

// project-level build.gradle.kts
plugins {
    id("com.google.gms.google-services") version "4.5.0" apply false
}

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.recyclerviewfirebase. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/recyclerviewfirebase/MainActivity.java

Adds users, listens to the database while visible, and feeds the list.

MainActivity.java
package com.example.recyclerviewfirebase;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * A live list of users from Firebase Realtime Database. Add a user (here or in the Firebase console)
 * and every phone running the app sees it appear instantly.
 */
public class MainActivity extends AppCompatActivity {
    private final DatabaseReference users = FirebaseDatabase.getInstance().getReference("users");
    private ValueEventListener listener;
    private MyAdapter adapter;
    TextView empty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screens.setUp(this, R.layout.activity_main);

        EditText name = findViewById(R.id.name);
        EditText email = findViewById(R.id.email);
        EditText age = findViewById(R.id.age);
        Button add = findViewById(R.id.btnAdd);
        empty = findViewById(R.id.textEmpty);

        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        adapter = new MyAdapter(user -> users.child(user.key).removeValue());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        add.setOnClickListener(v -> {
            String nameTXT = name.getText().toString().trim();
            if (nameTXT.isEmpty()) {
                name.setError(getString(R.string.need_name));
                return;
            }
            // push() creates a new unique key, so two users can share a name.
            users.push().setValue(new User(nameTXT, email.getText().toString().trim(), age.getText().toString().trim()));
            name.setText("");
            email.setText("");
            age.setText("");
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Listen only while the screen is visible, and stop in onStop to save battery and data.
        listener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                List<User> list = new ArrayList<>();
                for (DataSnapshot child : snapshot.getChildren()) {
                    User user = child.getValue(User.class);
                    if (user != null) {
                        user.key = child.getKey();
                        list.add(user);
                    }
                }
                adapter.submitList(list);
                empty.setVisibility(list.isEmpty() ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(MainActivity.this, R.string.error_read, Toast.LENGTH_LONG).show();
            }
        };
        users.addValueEventListener(listener);
    }

    @Override
    protected void onStop() {
        users.removeEventListener(listener);
        super.onStop();
    }
}

Full source code