Hello friends, in this Android Firebase Tutorial, we will see how we can make User Registration in Android by using Firebase as our App’s backend. I have already posted an Android Firebase Tutorial.
But firebase is now taken by google and things are changed a little. That is why I am again posting this Android Firebase Tutorial. Today we will learn about making User Registration using Firebase Authentication for Android. If you are interested in Swift and iOS then you can go through this Firebase iOS Tutorial.
Table of Contents
What is Firebase?
Firebase is a cloud service provider. It is now under google and this service can replace your whole server side part of your application. In many tutorials we had used MySQL and PHP for our database. But if we will use Firebase we do not need any server side code or configuration. We can directly use firebase. Firebase comes with a bundle of many features.
Features of Firebase?
Android Firebase Tutorial – Video
You can keep on reading this tutorial but if you don’t want reading, you can watch this video. This is not like my other videos. This is a complete explanation of this post. So you can go through this video as well.
Android Firebase Tutorial
So without wasting time lets start our Android Firebase Tutorial. We will start from creating a new project.
From this tutorial you will see screenshots of OS X. 😛 Thanks to you guys, I managed to get MacbookAir with your support. 🙂
Creating an Android Studio Project
- Open Android Studio and Create a New Project. I created FirebaseAuthDemo.
- Now wait for your project, once it is fully loaded we can start working.
- But before we need a app in Firebase Console as well. So switch to your browser from Android Studio.
Adding Firebase to our Project
- Go to firebase.google.com.
- Click on Get Started for Free.
- Now click on create a new project.Â
- Give your app a name and click on create project.
- Now you will see the Firebase Panel. You need to click on Android Icon here.
- Now you will see a panel here you need to put the package name of your project. You can get the package name from AndroidManifest.xml file. Copy the package name and paste it here and then click on Add App.
- When you click Add App, a file named google-services.json will be downloaded. You need to paste this file inside app directory of your project. See the below image for explanation.
- Now come inside your project level build.gradle file and modify it as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' //you need to add this line classpath 'com.google.gms:google-services:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } |
- Now modify your app level build.gradle file as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.0" defaultConfig { applicationId "net.simplifiedcoding.firebaseauthdemo" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0' } //add the following line apply plugin: 'com.google.gms.google-services' |
- Now sync your project with Gradle.
Adding Firebase Authentication
As in this Android Firebase Tutorial, we are building a user registration application, we need to use Firebase Authentication. So we need to add Firebase Authentication to our project. For this go inside app level build.gradle file and inside dependencies block add the following line.
1 2 3 4 5 6 7 8 9 10 |
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0' //add this line compile 'com.google.firebase:firebase-auth:9.2.1' } |
Again sync your project.
Enabling Email/Password Authentication
- Again go to your firebase panel.
- On the left side menu you will see Auth, click on it.
- Now click on Set Up Sign In Method.
- Â Now click on Email/Password, enable it and press save.
Creating User Signup Form
- Now we will need to create a form in our activity from where user can Sign Up to our app.
- Come inside your Android Studio Project, and in activity_main.xml write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="net.simplifiedcoding.firebaseauthdemo.MainActivity"> <LinearLayout android:layout_centerVertical="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/editTextEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:hint="Enter email" android:inputType="textEmailAddress" /> <EditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:hint="Enter password" android:inputType="textPassword" /> <Button android:id="@+id/buttonSignup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="Signup" /> </LinearLayout> </RelativeLayout> |
- The above code will generate the following UI.
Understanding Firebase Signup
This is the main part of this Android Firebase Tutorial. First I will tell you about the methods. As we will be using Firebase Authentication for the user Signup we need these steps to get it done.
Step #1
First we will define a FirebaseAuth object. Then inside method onCreate() we will initialize this object. See the following code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package net.simplifiedcoding.firebaseauthdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.google.firebase.auth.FirebaseAuth; public class MainActivity extends AppCompatActivity { //defining firebaseauth object private FirebaseAuth firebaseAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initializing firebase auth object firebaseAuth = FirebaseAuth.getInstance(); } } |
Step #2
Now we will call the method createUserWithEmailAndPassword() from the FirebaseAuth object. This method take two String parameters for email and password. And we attach a OnCompleteListener on it to check the task is completed or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private void registerUser(){ //creating a new user firebaseAuth.createUserWithEmailAndPassword("user email here", "user password here") .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { //checking if success if(task.isSuccessful()){ //display some message here }else{ //display some message here } } }); } |
Implementing Firebase Signup
We understood the code. Now lets implement it to make registration work in the activity. Write the following code in your MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package net.simplifiedcoding.firebaseauthdemo; import android.app.ProgressDialog; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; 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 { //defining view objects private EditText editTextEmail; private EditText editTextPassword; private Button buttonSignup; private ProgressDialog progressDialog; //defining firebaseauth object private FirebaseAuth firebaseAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initializing firebase auth object firebaseAuth = FirebaseAuth.getInstance(); //initializing views editTextEmail = (EditText) findViewById(R.id.editTextEmail); editTextPassword = (EditText) findViewById(R.id.editTextPassword); buttonSignup = (Button) findViewById(R.id.buttonSignup); progressDialog = new ProgressDialog(this); //attaching listener to button buttonSignup.setOnClickListener(this); } private void registerUser(){ //getting email and password from edit texts String email = editTextEmail.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); //checking if email and passwords are empty if(TextUtils.isEmpty(email)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(password)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } //if the email and password are not empty //displaying a progress dialog progressDialog.setMessage("Registering Please Wait..."); progressDialog.show(); //creating a new user firebaseAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { //checking if success if(task.isSuccessful()){ //display some message here Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show(); }else{ //display some message here Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show(); } progressDialog.dismiss(); } }); } @Override public void onClick(View view) { //calling register method on click registerUser(); } } |
- Now lastly add internet permission to your AndroidManifest.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.simplifiedcoding.firebaseauthdemo"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
- Now run your application.Â
- Click on Signup and if you got the success message. Check your firebase console.
- Bingo! the registration is working fine. You can get my source code from the link below.
[sociallocker id=1372] [download id=”3114″]Â [/sociallocker]
So thats all for this Android Firebase Tutorial friends. In the next post we will see about user login and other options like Password Reset, Email Change, Delete Account etc.
And yes feel free to leave your comments if having any confusions regarding this Android Firebase Tutorial. Thank You 🙂