Firebase in android studio with example

(Last Updated On: February 15, 2022)

Firebase in Android Studio with example:

Connecting Firebase in android studio- In today’s article, I will practically show you how to connect Firebase with Android Studio. I am not using firebase for the first time; Firebase has been my favorite IoT platform for monitoring sensors and for controlling electrical devices. I just love controlling things over long distances using internet as the communication medium. Today, Firebase is among the top IoT platforms, there are so many other IoT platforms like for example blynk application, Ubidots, Thingspeak, Telegram, IFTT, Google Assistant, etc. Each one of these IoT platforms has got it’s own pros and cons. I have used all of these IoT platforms with Arduino, ESP32, and ESP8266.

I have already used Firebase in some intermediate and advanced level projects including,

Firebase Android Application Designing using Android Studio, ESP32 DHT11

Nodemcu RFID Firebase, circuit diagram, and programming

ESP32 Firebase Tutorial, Send Sensor Data to Google Firebase Database

Arduino Firebase Database, Students Attendance system using RFID and GSM

You might be thinking why am I writing this article if I have used Firebase in such intermediate and advanced level projects? Well, today’s article covers the most basic things which include connecting Firebase with the Android Studio. If you don’t know how to design Android applications then you can read my articles on Android Application designing. After reading this article you will be able to connect your Android application with the Firebase using Android studio. Without any further delay, let’s get started!!!


Creating a new Firebase project in Android studio:

First, click on the Android studio icon and it will open up the Android studio

firebase in android

Then click on the New Project button in the Android studio

firebase in android

Then select the empty activity and press the next button

firebase in android

Then provide an application name it doesn’t really matter any name is fine in my case I set the name as FirebaseCrud, and then click on the finish button

firebase in android



After clicking the finish button you will be taken into a window like this

firebase in android

Connect Firebase to Android Studio:

Now once your project successfully launched what you want to do is go to the tools menu click on it, it will show a dropdown menu among all the options click on the firebase.

firebase in android

When you click on the firebase, you will see a new firebase panel will open.

firebase in android

In this panel, you will see a lot of options, but none of them are important to us right now you can see a lot of options like analytics, cloud messaging, authentication, real-time database storage, etc and all that stuff but right now our main focus is on the real-time database. So click on the real-time database, when you clicked on the real-time database it will show you the save and retrieve data link button so you must click on this link as you can see in the below figure

firebase in android


After clicking the save and retrieve data link button, it will show you the save and retrieve data panel in this panel click on connect to firebase as you can see in the below image

firebase in android

When you clicked the connect to firebase button it will a dialog box, simply click build and wait for the sync process

firebase in android

firebase in android

When the build process is finished, press once again the “connect to firebase” button as shown in the below image

firebase in android


After clicking the “connect to firebase” button, you will be taken into a default web browser and you make sure you have logged in into your Google account.

firebase in android

When the firebase database website loaded successfully, simple click on create a project

firebase in android

It will detect your android studio project name automatically so click on the continue button as you can see in the below image

firebase in android

Again click on the continue button

firebase in android


Checked the I accept the google analytics terms and click create project button

firebase in android

So wait for the project creating process it will take a while

firebase in android

After the project creating process simply click the continue button

firebase in android

It will show you a dialog with connect button so click on connect button

firebase in android

After clicking the connect button it will show the message “your android studio project is connected to your firebase Android app”

firebase in android

In the android studio, you will see connected in green color this means that your app is successfully connected

firebase in android



After that click on “add the realtime database SDK to your app” button

firebase in android

When you clicked the button, it will show you a dialog box, simply click on accept changes it will add the dependencies in your app Gradle file

firebase in android

Wait for adding dependencies process it will take time, it depends on the internet connection

firebase in android

When the process is finished, you will see the “dependencies set up correctly” in green color which mean your dependencies are added successfully

firebase in android

When the firebase connecting process is completed, you will see the firebase webpage like this

firebase in android


Firebase Database access Configuration for the Android Studio:

Now we want to configure our access for that you will need to change the default rules by default it is set to false, simply change it to true.

Select Realtime database in the left side panel and click on create the database

firebase in android

then click the next button

firebase in android

Then checked the start in test mode, and  click on Enable

firebase in android

Click on the rules tab and from there change the configuration into true now once everything is done click publish

firebase in android


see rules are published

firebase in android

Make sure to use the firebase real-time database version which available on the official firebase website

firebase in android

firebase in android

status so I’m creating a toast here that says firebase connection success so that our connection is successful and it will show the toast now let me test my code let me alone Jim later first might take a while okay now you can see that firebase connection was successful

now I am writing a simple toast message code to test the connection status of our firebase in android studio.



Simple firebase toast message program:

firebase in android

Simply I create a toast message method here that will show firebase connected successfully message on the screen. Now runs the application to check application is successfully connected with firebase or not, simply click on the run button

firebase in android

As you can see I connected firebase database with android studio successfully

firebase in android


 

Creating Crud Application using firebase in android studio:

Now I am going to show you how to insert data into firebase in android studio. So I edit the existing project.

I have made a simple application layout as you can see in the below image

firebase in android

Firebase in android studio programming:

activity_main.xml code:

here I created constraintlayout, in this layout I create one insert data button and four editText field name, last name, age, and phone number and I set its attribute as per my requirement. If you want to improve its design so change its attributes. The code is pretty simple so I cannot explain it.


MemberData.java Code:

First, create a new java class named MemberData for getter and setter

firebase in android

Then generate the getter and setter methods simply press the Alt+insert button, it will show a menu in this select the getter and setter methods

firebase in android

After clicking the getter and setter methods, again it will show a dialog box in this dialog box select all fields and press ok button

firebase in android


It will generate the below code automatically

MainActivity.java code:



Firebase in android studio Program explanation:

In the MainActivity class, First, I initialized variable names for the EditText and button. Then I initialized name for DataBaseReference method.  Then I initialized the name for MemberData class.

here I can use this to refer to our actual layout that I have created earlier. now you can use a findViewById function to point each variable to our fields.

Then I created another reference variable to connect to our database

then I call the MemberData class which I have created for getter and setter methods

then I have created the onclicklistener event. Inside this event, I pass all the variables and their type.

Firebase in android studio final output:

now run the firebase application

firebase in android

Fill the field and press the insert data button, it will show a toast message data inserted as you can in the below image

firebase in android


As you can see all the data is inserted successfully in the firebase database.

firebase in android

Could not parse Error fixing:

When you are getting “could not parse the android application module’s Gradle config. Resolve Gradle build issues and/or resync”  error while connecting to firebase in android studio.

firebase in android

For that simply open module-level build Gradle.

firebase in android

Then change the compileSdk version, in my case, my compileSdk version is 32 and I changed it to 31.

firebase in android



And press the sync now button.

firebase in android

With these changes, you will not get this “could not parse the android application module’s Gradle config. Resolve Gradle build issues and/or resync”  error message again. So, that’s all for now.

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: