Introduction to Firebase
Firebase is a simple cloud-based Google service to avoid server side scripting with a lot of built-in features like authentication with social media platforms like Facebook, Twitter, and Google. It comes up with many more features like Realtime database, Storage features, Cloud messaging, hosting, remote configuration, app indexing and all other Google features for marketing and development. Firebase is also taking over the GCM (Google Cloud Messaging) features as it has real-time update feature with GCM. It also supports JavaScript features including support for AngularJS, IOS, and Android.
Getting Started with Firebase
Before getting started with Firebase, you must get yourself registered with the Firebase.
Download Firebase Android Studio Sign in/Sign up Demo Project
Register with Firebase
- Open Firebase Google Console and Click on GET STARTED FOR FREE. You will need to login to your Google Account if you already have one or you must create a new Google account.
- Now CREATE A NEW PROJECT, name your first project and click CREATE PROJECT.
First Firebase Project - Create a new Project in Android Studio.
- Click on Add Firebase to Your Android App. Find the package name of your project and Add App to Firebase. This will download google-services.json for your Android App.
Add App to Firebase - Set the project directories to Project view from Android in Android Studio.
Switch to Project view in Android Studio - Drag and Drop google-services.json in the app directory of your project.
Drag and Drop Google-Service.json in the App Directory - It will ask to rename the file if required. Leave it to as is and press OK to add the file to the root directory of your project.
- Move forward and click finish to Add App to firebase.
Application added to Firebase
Setting Up Dependencies
Add Classpath
Open build.gradle (Project: Firebase) and add classpath in the dependencies and press Sync Now link at the top-right end of the file. This will synchronize your project.
1 |
classpath 'com.google.gms:google-services:3.0.0' |
1 2 3 4 5 6 7 |
dependencies { classpath 'com.android.tools.build:gradle:2.1.2' 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 } |
Apply Plugin
Now in the build.gradle (Module: App) apply plugin for google-services.
1 |
apply plugin: 'com.google.gms.google-services' |
Add Firebase Authentication
In build.gradle (Module: App) also add a dependency for Firebase Authentication.
1 |
compile 'com.google.firebase:firebase-auth:9.2.1' |
Complete code for build.gradle (Module: App)
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 |
apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicationId "com.example.muneeb.firstfirebase" minSdkVersion 13 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.1.0' compile 'com.google.firebase:firebase-auth:9.2.1' } |
Again Sync your project for synchronization.
Enable Email and Password Authentication from Firebase Dashboard
Open Firebase dashboard, Click on Auth link at the left side of the dashboard menu and SET UP SIGN-IN METHOD. Select and enable Email/Password Sign-in.

Download Android Studio Configured Sign in/Sign up Demo Project
Sign-up/Register User
Create Sign-up Form
To register a user create a sign-up form so that the users can put up their credentials and can log-in to your application.

Complete XML Code for Sign-up Form
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 |
<?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: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="com.example.muneeb.firstfirebase.MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:id="@+id/email" android:layout_marginTop="180dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/password" android:layout_alignEnd="@+id/password" android:hint="Enter Email Address"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/password" android:layout_below="@+id/email" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginTop="40dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:hint="Enter Password"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sign Up" android:id="@+id/button" android:layout_below="@+id/password" android:layout_centerHorizontal="true" android:layout_marginTop="79dp" android:onClick="signup"/> </RelativeLayout> |
Implement the Button onClick Listener and Get Text from Text Fields
Now in the Java file of your project, create button onClick handler and get Text from the email and password fields. You must check whether the email and text fields are empty or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public void signup(View v) { //get text fields EditText Email=(EditText) findViewById(R.id.email); EditText Password=(EditText) findViewById(R.id.password); //get data from text fields String userEmail=Email.getText().toString(); String userPassword=Password.getText().toString(); //check if the fields are empty if(TextUtils.isEmpty(userEmail)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(userPassword)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } } |
Register User with Firebase Auth
You will need an instance of FirebaseAuth to register a user with FirebaseAuth.
1 |
FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); |
implement createUserWithEmailAndPassword(email, password) method along with the addOnCompleteListener(). The response will be returned in onComplete() overridden method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Register user with firebase firebaseAuth.createUserWithEmailAndPassword(userEmail, userPassword) .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 Failed",Toast.LENGTH_LONG).show(); } } }); |
You can also display progress dialog until the registration process is carried out (It is not necessary to show the progress dialog).
1 2 3 4 5 6 |
//instantiate progress dialog final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); //Show waiting dialog box until user is registered progressDialog.setMessage("Registering User..."); progressDialog.show(); |
Includes for FirebaseAuth Application
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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.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; |
Complete Code for Sign-up
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 |
public void signup(View v) { //get text fields EditText Email=(EditText) findViewById(R.id.email); EditText Password=(EditText) findViewById(R.id.password); //get data from text fields String userEmail=Email.getText().toString(); String userPassword=Password.getText().toString(); //check if the fields are empty if(TextUtils.isEmpty(userEmail)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(userPassword)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } //instantiate progress dialog final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); //Show waiting dialog box until user is registered progressDialog.setMessage("Registering User..."); progressDialog.show(); //FirebaseAuth instance FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); //Register user with firebase firebaseAuth.createUserWithEmailAndPassword(userEmail, userPassword) .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 Failed",Toast.LENGTH_LONG).show(); } progressDialog.dismiss(); } }); } |

Sign in/Login User
Modify the Sign-up Form
To use the user credentials for signing in you will also need FirebaseAuth. For the ease of the user, you can use the same sign-up form and modify it as below so the user can sign in with the same sign-up form. So now the Sign-in and Sign-up form will look like.

Complete XML Code for Sign-in/Sign-up Form ‘activity_main.xml‘
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 |
<?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: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="com.example.muneeb.firstfirebase.MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:id="@+id/email" android:layout_marginTop="180dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/password" android:layout_alignEnd="@+id/password" android:hint="Enter Email Address"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/password" android:layout_below="@+id/email" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginTop="40dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:hint="Enter Password"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sign Up" android:id="@+id/button" android:onClick="signup" android:layout_below="@+id/password" android:layout_alignRight="@+id/password" android:layout_alignEnd="@+id/password" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Already Registered?" android:id="@+id/textView" android:layout_below="@+id/button" android:layout_alignRight="@+id/button" android:layout_alignEnd="@+id/button" android:layout_marginTop="40dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sign in with Above Credentials" android:id="@+id/button2" android:layout_below="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" android:onClick="signin" /> </RelativeLayout> |
Implement onClick Listener
Create the button click listener in the Java file so that when the user enters the credentials and press the SIGN IN button, it will get the credentials from the text fields and authenticate the user begore signing in. You can follow the same procedure to get the text from the text fields or you can also declare the text fields with class level scope and initialize them in onCreate() method. I am separately declaring and initializing the text fields for more clarity of the 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 |
public void signin(View v) { //get text fields EditText email=(EditText) findViewById(R.id.email); EditText password=(EditText) findViewById(R.id.password); //get data from text fields String userEmail=email.getText().toString(); String userPassword=password.getText().toString(); //check if the fields are empty if(TextUtils.isEmpty(userEmail)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(userPassword)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } //instantiate progress dialog final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); //Show waiting dialog box until user is registered progressDialog.setMessage("Logging In..."); progressDialog.show(); //FirebaseAuth instance FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); } |
Authenticate and Login User with Firebase Auth
implement signInWithEmailAndPassword(email, password) method along with the addOnCompleteListener(). The response will be returned in onComplete() overridden method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//logging in the user firebaseAuth.signInWithEmailAndPassword(userEmail, userPassword) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { progressDialog.dismiss(); //if the task is successfull if(task.isSuccessful()){ //start the profile activity Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show(); finish(); startActivity(new Intent(getApplicationContext(), HomeActivity.class)); } else { Toast.makeText(MainActivity.this,"Invalid Credentials",Toast.LENGTH_LONG).show(); } } }); |
Now the complete java code for Login Button listener will be
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 |
public void signin(View v) { //get text fields EditText email=(EditText) findViewById(R.id.email); EditText password=(EditText) findViewById(R.id.password); //get data from text fields String userEmail=email.getText().toString(); String userPassword=password.getText().toString(); //check if the fields are empty if(TextUtils.isEmpty(userEmail)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(userPassword)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } //instantiate progress dialog final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); //Show waiting dialog box until user is registered progressDialog.setMessage("Logging In..."); progressDialog.show(); //FirebaseAuth instance FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); //logging in the user firebaseAuth.signInWithEmailAndPassword(userEmail, userPassword) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { progressDialog.dismiss(); //if the task is successfull if(task.isSuccessful()){ //start the profile activity Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show(); finish(); startActivity(new Intent(getApplicationContext(), HomeActivity.class)); } else { Toast.makeText(MainActivity.this,"Invalid Credentials",Toast.LENGTH_LONG).show(); } } }); } |
Create Demo Home Screen For the User
You will also need a demo home screen, so that when the user is logged in successfully, he/she must be directed toward another home screen activity for registered and logged in users. A LOGOUT button must be there for the user to log-out.

Complete Home Screen ‘activity_home.xml’ 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 |
<?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: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="com.example.muneeb.firstfirebase.HomeActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Logout" android:id="@+id/button3" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:onClick="logout"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Howdy!" android:id="@+id/textView2" android:layout_alignBottom="@+id/button3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/EmailUser" android:layout_below="@+id/button3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> |
Implement Click Listener for LOGOUT button
To logout user, you need to call signOut() method of Firebase class.
1 2 3 4 5 6 7 8 9 10 11 |
public void logout(View v) { //logout user firebaseAuth.signOut(); //finish home screen activity finish(); //starting main activity startActivity(new Intent(this, MainActivity.class)); } |
Check if the User is not Signed in
1 2 3 4 5 6 7 8 |
//check if the user is not logged in if(firebaseAuth.getCurrentUser() == null){ //closing current home activity finish(); //start Main activity startActivity(new Intent(this, MainActivity.class)); } |
Get the Email of Logged in User
1 2 3 4 5 6 |
//Get Firebase logged in user FirebaseUser firebaseuser = firebaseAuth.getCurrentUser(); //set email address on screen for the current logged in user TextView emailuser= (TextView) findViewById(R.id.EmailUser); emailuser.setText(firebaseuser.getEmail().toString()); |
Forget Password Email for Registered Users
Similarly, Firebase also provides a wonderful feature to recover the password. incase user forgets the password. Firebase automatically sends the password reset link to the registered email address. You will need to provide the email address for which the user wants to reset the password. To send the recovery email sendPasswordResetEmail(email) method of FirebaseAuth class is used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//FirebaseAuth instance FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); //Send Password Reset Link firebaseAuth.sendPasswordResetEmail(email) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ResetPasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show(); } progressBar.setVisibility(View.GONE); } }); |
Complete Code for HomeActivity.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 |
package com.example.muneeb.firstfirebase; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; public class HomeActivity extends AppCompatActivity { FirebaseAuth firebaseAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //initializing Firebase user firebaseAuth = FirebaseAuth.getInstance(); //check if the user is not logged in if(firebaseAuth.getCurrentUser() == null){ //closing current home activity finish(); //start Main activity startActivity(new Intent(this, MainActivity.class)); } //Get Firebase logged in user FirebaseUser firebaseuser = firebaseAuth.getCurrentUser(); //set email address on screen for the current logged in user TextView emailuser= (TextView) findViewById(R.id.EmailUser); emailuser.setText(firebaseuser.getEmail().toString()); } public void logout(View v) { //logout user firebaseAuth.signOut(); //finish home screen activity finish(); //starting main activity startActivity(new Intent(this, MainActivity.class)); } } |
Complete Code for ActivityMain.xml
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
package com.example.muneeb.firstfirebase; import android.app.ProgressDialog; import android.content.Intent; 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.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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void signup(View v) { //get text fields EditText Email=(EditText) findViewById(R.id.email); EditText Password=(EditText) findViewById(R.id.password); //get data from text fields String userEmail=Email.getText().toString(); String userPassword=Password.getText().toString(); //check if the fields are empty if(TextUtils.isEmpty(userEmail)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(userPassword)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } //instantiate progress dialog final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); //Show waiting dialog box until user is registered progressDialog.setMessage("Registering User..."); progressDialog.show(); //FirebaseAuth instance FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); //Register user with firebase firebaseAuth.createUserWithEmailAndPassword(userEmail, userPassword) .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 Failed",Toast.LENGTH_LONG).show(); } progressDialog.dismiss(); } }); } //-----------------------------------------------------------Signin Code------------------------------------------------------- public void signin(View v) { //get text fields EditText email=(EditText) findViewById(R.id.email); EditText password=(EditText) findViewById(R.id.password); //get data from text fields String userEmail=email.getText().toString(); String userPassword=password.getText().toString(); //check if the fields are empty if(TextUtils.isEmpty(userEmail)){ Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); return; } if(TextUtils.isEmpty(userPassword)){ Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); return; } //instantiate progress dialog final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); //Show waiting dialog box until user is registered progressDialog.setMessage("Logging In..."); progressDialog.show(); //FirebaseAuth instance FirebaseAuth firebaseAuth=FirebaseAuth.getInstance(); //logging in the user firebaseAuth.signInWithEmailAndPassword(userEmail, userPassword) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { progressDialog.dismiss(); //if the task is successfull if(task.isSuccessful()){ //start the profile activity Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show(); finish(); startActivity(new Intent(getApplicationContext(), HomeActivity.class)); } else { Toast.makeText(MainActivity.this,"Invalid Credentials",Toast.LENGTH_LONG).show(); } } }); } } |
