Categories: Android

Getting Started With Firebase in Android

Greetings People!

Today I am going write about Firebase, which will be targeting mostly the people who don’t know what Firebase is.

What is Firebase?
Before Google owned Firebase. It was real-time database. Now it has many other features like Cloud Messaging, Storage and etc. Read more

Lets start with making a simple application. Open https://console.firebase.google.com/. Login with your google account. Click Create New Project add name and location to it then click Create Project.

You’ll be redirected to your project’s dashboard. Then click on Add Firebase to your Android app

Add your package name. I have already created a project so I added its name. Then click Add App.

After clicking that you’ll get a google-services.json file.

Open your project in Android Studio and in the Explorer paste your google-services.json file under app folder.

In your Project build.gradle.

And in your Module build.gradle.

Click on your Database then click Rules. Make Read and Write both to true.

After that open your activty_main.xml and add a Edit text and a Button, and reference in a the MainActivity.

Create Firebase database object by using the following code:

 FirebaseDatabase database = FirebaseDatabase.getInstance();
 final DatabaseReference myRef = database.getReference("Sample Firebase");

Now its time to set values to Firebase. In your button listener.

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//Get the text from Edit text
                String text = editText.getText().toString();
//set it on Firebase                
myRef.setValue(text);
            }
        });

In order to listen to changes to Firebase.

 myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
//Invokes whenever data is updated or changed
                String value = dataSnapshot.getValue(String.class);
                Toast.makeText(getApplication(),"Value Changed To:"+value,Toast.LENGTH_SHORT).show();
                
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

With this being done. You’re all set up and now ready to set data to Firebase.
Run your project and test by sending data.

Testing on Simulator.

You can find this project on Github.
If you have any questions leave a comment. I’ll get back to you as soon as I can. 🙂

See ya!

Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he's probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

View Comments

  • You actually make it seem really easy together with your presentation however I to find this topic to be actually something that I believe I might never understand.
    It sort of feels too complex and very vast for me. I am having a look ahead on your
    next publish, I'll attempt to get the grasp of it!

  • First off I want to say terrific blog! I had a quick question which I'd like to ask if you do
    not mind. I was interested to know how you center yourself and clear your thoughts prior to writing.
    I've had a difficult time clearing my thoughts in getting my thoughts
    out. I do enjoy writing however it just seems like the first 10 to
    15 minutes are generally lost just trying to figure out how to
    begin. Any suggestions or tips? Thanks!

    • Hi,
      Really appreciate your kind words.
      When I started writing blogs, I had a purpose which was to not let another developer go through the same pain of implementing something as I did. So this what helps and motivates me to write. Before writing I start to research on the thing I am going to write on, and create a sample project. After I'm done with the sample app, more than half of my work is done then all I have to do is come up with some words to explain what I have done. Hope this helps.
      Cheers!

  • I don't even know how I ended up here, but I thought this post was great.
    I do not know who you are but certainly you are going to a famous blogger if you
    aren't already ;) Cheers!

  • Hello my family member! I want to say that this article is amazing, great written and come
    with almost all significant infos. I'd like to see more posts like
    this .

  • Hurrah! In the end I got a website from where I know how to really get valuable data regarding my study and knowledge.

  • Your style is sο unique inn comparison tо otһeг folks I have read stuff from.

    Ι aρpreciate yоu for posting ԝhen you've got the opportunity,
    Guess Ӏ'll ϳust book mark this blog.

  • I have to thank you for the efforts you have put in penning
    this site. I am hoping to see the same high-grade content by you in the
    future as well. In truth, your creative writing abilities has encouraged
    me to get my own site now ;)

  • I've been surfing online more than three hours today,
    yet I never found any interesting article like yours. It's pretty worth
    enough for me. Personally, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Factory Design Pattern

In this article, you will learn about a type of Creational Design Pattern which is…

5 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago