firebase android

Getting Started With Firebase On Android

Firebase quick start

As you all know, Firebase is the Future of Android apps, In this world where everything changes in an instant, It is important to stay updated with the latest skills!

In this post, I am going to show you guys, How to start building android apps with Firebase!

Before we get to the coding part, you will need a Gmail account, which you easily make one. If you haven’t already, then by visiting  Google Account Sign up you will get a new google account.

  1. Next step is to make to make a new Android Studio Project. If you don’t know, how to do that you can refer to this video below.
    After you make your project, open this link and sign in using your Gmail account. After that click on the create new project button.1
  2. And a pop-up box like this should appear2
  3. Now enter the name of your Project and select your country. In my case I will just call it Test after you are done by filling the name and selecting your Country click on Create project and you should get redirected to this page.Codegeek.io
  4. Now click on add Firebase to Your android app and Enter your Package name In my case it happens to be io.codegeek.test and then click on Add APP Button.4
  5. Now you should be prompted by this window and a download window. You can either choose to download if you want or you can just skip to console, I am going to skip to console because we can add dependencies in Android studio just  using few clicks.5
  6. Now open android studio and go to project structure and select cloud and check the box like in the screen shot below and click on Ok and wait for the sync to get completed.6
  7. Now Open your Mainactivity.java file and add onStart Function in it, after onCreate Function like this.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    }
    
    @Override
    protected void onStart() {
        super.onStart();
    }
    
    
  8. After that Call Firebase in your Main activity as mRef (you can change mRef with whatever you want).
    Firebase mRef;
    

    and then assign your project URL to mRef like this in onStart Function.

    mRef = new Firebase("Change this with your URL");

    If you don’t have the URL, you can get it by going to firebase console  And don’t forget to change your rules to true Like this it will let u read and write data from the database.

    codegeek.io

  9. Before we complete Java part make sure that you add this code to your layout file
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sunny"
        android:id="@+id/sunny"
        android:layout_marginTop="172dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="27dp"
        android:layout_marginStart="27dp" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Foggy"
        android:id="@+id/foggy"
        android:layout_alignBottom="@+id/sunny"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="51dp"
        android:layout_marginEnd="51dp" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Weather"
        android:id="@+id/weather"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />
    
  10. Now all we need to do is some basic stuff first call buttons and textview in onstart function like this
    weather = (TextView) findViewById(R.id.weather);
    sunny = (Button) findViewById(R.id.sunny);
    Foggy = (Button) findViewById(R.id.foggy);
  11. Now copy and paste this code below that one ^
    mRef.addValueEventListener(new ValueEventListener() {
    
    // This method will execute whenever data is changed in database and this will set the vale of textview to the data which got changed
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String data = dataSnapshot.getValue(String.class);
    
            weather.setText(data);
        }
    
        @Override
        public void onCancelled(FirebaseError firebaseError) {
    
        }
    });
    
    // this will update the database whenever sunny button is pressed
    
    sunny.setOnClickListener(new View.OnClickListener() {
                                 @Override
                                 public void onClick(View v) {
    
                                     mRef.setValue("Sunny");
                                 }
                             }
    
    );
    // this will update the database whenever Foggy button is pressed
    Foggy.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
    
     mRef.setValue("Foggy");
     }
     }
    );

Don’t forget to comment and share if you liked this article on Firebase android.

Leave a Reply