Image Source: Link

Introduction

My name is Moses N. I am a WordPress developer based in Kenya. Before I proceed, I know you are curious how come am a WordPress developer and am writing about Firebase, nevertheless, am currently venturing into android application development, and in this tutorial will be my pleasure to show you how to generate real-time reports using Firebase.

How to generate real-time reports using Firebase will be a continuation of How to create an android two-step authentication signup system using Firebase. So I would urge you to look at it before we continue. What will be actually doing in this tutorial is:

  1. Will be adding a button “Report” to our home activity.
  2. Button report will be used to save reports on our android phone and also display.
  3. iText Pdf Library will help us create PDF.
  4. Handle runtime permission.
  5. We will be retrieving information from the Firebase database.
  6. Will be designing our reports in a table format.
  7. Will be allowing write and read permission from our storage.

Get the complete project from GitHub
Enough intro? Let’s dig in now.

Photo Preview

Glossary

  • Reports A report is a document that presents information in an organized format for a specific audience and purpose.
  • Firebase – is a Backend-as-a-Service (Baas) because It provides developers with a variety of tools. Also, services to help them develop quality apps, grow their user base, and earn a profit.
  • Real-timethe actual time during which a process or event occurs.

In conclusion, I believe we are on the same page now. Therefore, let’s take our time to talk about the Project Requirement for Firebase Real-time Reports.

Firebase Real-time Reports project requirement.

  1. Android Phone – you will require your android phone to act as an emulator for running the project. Alternatively, you can download Memu. Also, you can install the emulator from your android studio.
  2. Editor – We will need an editor to write our codes with. You can go online and check out any text editors but I recommend we stick to Android Studio.
  3. Internet Connectivity – we will require the internet all through our project for installing dependencies and accessing Firebase Database.
  4. Git – This is kinda optional but I still recommend having git installed. This will prove useful if you ever want to deploy your code or push to a remote repository. You can download and install git through Git.

In short, that’s our project requirement. Let’s begin coding!

Steps to Firebase Real-time Reports:

Step 1: Adding Report Button.

Since we had designed our home activity in the previous blog we are going to change only a few things to make look at the photo preview below.

How to generate real-time reports using Firebase

Below is the Home Activity code.

Step 2: Adding Firebase Real-time Reports Project dependencies.

We will be using the iText PDF Library to create PDF. You can get the library from here. To view the PDF we will need another library barteksc PDF viewer. You can get from here.

//for pdf
    implementation 'com.itextpdf:itextg:5.5.10'
    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

Step 3: Allowing Read and Write permission.

How to generate real-time reports using Firebase

In order to save our PDF report in our phone storage, we will need to allow permission from the manifest. We will also allow handle runtime permission.

  • Manifest permission.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • Handling runtime permission.
    public static String[] PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    public static int PERMISSION_ALL = 12;
 public boolean hasPermissions(Context context, String... permissions) {
        if (context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
 public void previewDisabledUsersReport()
    {
        if (hasPermissions(this, PERMISSIONS)) {
            DisplayReport();
        } else {
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
    }

Step 4: Creating setters and getters.

We are going to create a java class to hold all the setters and getters. The getters will enable us to retrieve data from the Firebase database and the setters will help set the retrieved data in the report.

Step 5: Creating the PDF report

Here we are going to create a method that will allow us to design the Firebase Real-time Reports. We will determine the number of columns it has from the data we are fetching from the database. We will also add a header and a footer. Now with that said let’s design the report to look like the image below.

How to generate real-time reports using Firebase

Step 6: Fetching data from Firebase.

In this stage, we will fetch data from the Firebase database using getters. Then we will display the data fetched in the report using setters.


Finally, we will save the report in internal storage.

Step 7:Display report.

Finally lets display the report on click or the report button.

.

full code for the main activity

.
By this, we have completed our tutorial on how to generate real-time reports using Firebase. Click on the Run button and Run the application on your android phone but you can also install an android emulator on the android studio.

Reflective Analysis

It was a simple project for me, but I was still able to gain some deeper insights into Firebase and programming in general. Working with imported dependencies was a challenge but managed to fix by visiting stack overflow and you-tube tutorials.

Future Directions

In order to get more knowledge about Firebase Real-time Reports, I recommend that you add more rows and columns in the report. You can try using different fonts and changing the color of the report to your choice. You can also try to generate reports using SQLite.

Learning Strategies and Tools

There are a lot of learning tools online, I would recommend the following:

I used the learning tools above to create Firebase Real-time Reports. I also used PDF documentations to achieve this. Anytime I faced some bugs, I would use stack overflow to check for solutions.

It took me a total of 9 hours to finish the project and the blog.

Conclusion

In conclusion, this project mainly focuses on how to generate real-time reports using Firebase. Reports are very important in each and every organization for many reasons. I, therefore, find this project very useful. . I hope you have learned a lot from this lesson. Therefore, I will be looking forward to seeing you build something great.
Get the complete project from GitHub
Happy Coding!