NATIVE ANDROID | CREATE FIREBASE PROJECT | ADD FIREBASE WITH EXAMPLE

Share the post

CREATE FIREBASE PROJECT | NATIVE ANDROID

In the last topic, we have seen How to create a firebase project in detail. In today’s topic, we will look into How to add firebase into the android application? Also, we will see one example where user can register into our android application & we will authenticate that user with help of google firebase.
We will see one example to implement firebase functionality to our android application.

Please follow the below steps to implement the same:

1. Create a simple android project using android studio.

2.  Add google service dependency :

Please find the below image for more details :
Google_gms_dependency

3.  After that  add the app dependency :

Now, add the firebase auth dependency in the Gradle file as shown below image :
Add_Firebase_Auth_Dependency

4. Add Plugin :

To implement the google gms plugin write the below line on the Gradle file. Please see the below image :
gms_plugin

5. Add Internet Permission :

It’s necessary that if you want to communicate in real-time with google firebase you must have to add internet permission to your android application. This permission can be added using the manifest file of the android application. Please see the below image for more details :
Add_Internet_Permission

6. Write MainActivity.java file code as given below :

       


import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText editTextEmail;
    private EditText editTextPassword;
    private Button buttonRegister;
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        progressDialog = new ProgressDialog(this);
        firebaseAuth = FirebaseAuth.getInstance();
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        buttonRegister = (Button) findViewById(R.id.buttonRegister);
        buttonRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == buttonRegister){
            registerUser();
        }
    }

    private void registerUser(){
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
        if(email == ""){
            Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
            return;
        }

        if(password == ""){
            Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
            return;
        }

        progressDialog.setMessage("Registering User...");
        progressDialog.show();

        firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    //user registered successfully
                    Toast.makeText(MainActivity.this,"Registered successfully",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this,"Something went wrong...please try again",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}


	

7. Write activity_main.xml file code as given below :

       
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">

    <EditText
    android:id="@+id/editTextEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter email id"
    android:inputType="textEmailAddress"/>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter password"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/buttonRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register User"
        android:layout_margin="15dp"/>

</LinearLayout>

8. Add the google-services.json file root folder :

In the previous topic, We have seen that how to create google-services.json & How to add that file into the project. Kindly click here for more details.

9. Go to firebase console & give the necessary permission :

As per our code, we are registering users with email & password. So, We have to give email & password sign in/up permission in the google firebase console. To do that follow the below steps :
  • Login to the firebase console.
  • Go to the Authentication menu.
  • Click on the Sign-in method menu.
  • Click on Email/Password.
  • Toggle the button & on that service
Please see the below image for more details :
Enable_Email_Password
Now, you have enabled the email/password authentication service in the firebase console. Finally, Click on save.

10. Run the application & Sign Up the New User :

After running the app write email id as a@a.com & password as123 click on the Register User button.
Make sure that your app is connected to the internet & after that you can able to see the newly created user in the google firebase console under Authentication ==> User Section as shown in the below image :
output
That’s it. These are the steps you have to do for communication with the firebase console. These are the very easy steps to add firebase to the android project.

Leave a Comment