Firebase Phone Authentication Android Tutorial

Today we will learn about Firebase Phone Authentication. Firebase is fantastic, and it provides almost the features that you need for the backend of your application. So in this tutorial, we will learn about Firebase Phone Authentication, and I am pretty sure that you have already seen this thing in many apps. You enter your phone number, receives an OTP, and then you use that OTP to authenticate.

Firebase Phone Authentication Video

  • If you are more comfortable in watching video, where you will see me doing all the stuffs in real then you can skip this post and watch the video instead.

  • Don’t forget to SUBSCRIBE to Simplified Coding on YouTube. 

Benefits of using Phone Authentication

When you use phone number authentication, you have many benefits like

  • Preventing Fake Users: When you use phone authentication, the user can’t be able to register for multiple accounts, as for each account a unique phone number is needed.
  • Increase User Value: When you have all the users verified by a phone number the value of your user base increases.
  • Increased Security and User Experience:Nowadays more people using apps and remembering passwords are a headache for many users, so they end up using weak passwords. Using phone authentication increases security and user experience, as the user does not need to create and remember passwords, they will enter their number and then they can receive a temporary authentication code by SMS.

Why Firebase Phone Authentication?

For implementing phone authentication you need to pay for SMS service, but with firebase, you can do it for FREE, isn’t it awesome? The free plan of firebase has Ten Thousand Verification per month.  Thats enough for the starter apps I guess, but yes if you exceed this limit, you need to pay.

So, guys enough for the discussion now let’s start our project and let’s see how we can integrate phone authentication in our application.

Firebase Phone Authentication Tutorial

Now let’s learn how to implement Firebase Phone Authentication in our project.

Creating a new Android Studio Project

  • Again we will do it in a new project, so create a new Android Studio project. In my case I have created a project named FirebasePhoneAuthentication.
  • Once the project is completely loaded, we will add firebase authentication into it.

Adding Firebase Authentication

  • Click on tools -> firebase. It will open an assistant from where you can add all the firebase services into your project.
  • So from here you can create a new firebase project or you can also select an existing project.
  • Connect your android project to firebase project and add the dependencies using this assistant.

If you are confused about this step please follow this Firebase Cloud Messaging Tutorial where I explained all the steps about adding firebase to android project. 

Enable Firebase Phone Authentication

  • To do this go to Firebase Console and open the project that you are using.
  • Then go to Sign In Method and enable Phone Authentication.

Enabling Firebase Phone Authentication

Designing Screens

Before moving into designing I would like to tell you that, for Sign In we will have two activities, in the first activity the user will enter his phone number and in the next we will verify the code sent by SMS.

Enter Phone Number Screen

  • I have created this design for this Screen.

firebase phone authentication 1

  • For designing the above screen come inside activity_main.xml and write the following xml code.

  • The above xml file will give you some errors as I have used some drawable resources, but don’t worry you can get the resources in my source code and the link of the source code is at the bottom, so keep reading. 

Verify Phone Number Screen

Now we will create the next activity where we will very the phone number. In this screen as well we need an EditText where user will input the code. But we will detect the SMS automatically so user do not need to enter manually.

  • First create a new Activity in the project, and I have created an activity named VerifyPhoneActivity.

firebase phone authentication 2

  • For the above design for your VerifyPhoneActivity, open the activity_verify_phone.xml and write the following xml code.

  • Now we will create one more activity named ProfileActivity.

Creating ProfileActivity

  • Create a new EmptyActivity named ProfileActivity and this activity will open after the successful login.
  • In this screen we have nothing but only a TextView with a welcome message, as you can see below.

firebase phone auth tutorial

Getting the Mobile Number

  • We will get the mobile number of the user in MainActivity. So come inside and write the following code.

  • The above code is very straightforward. We are just taking the mobile number from EditText and sending it to VerifyPhoneActivity.class with intent.

Verifying Mobile Number

Sending Verification Code

  • For sending verification code we will use the following code.

  • sendVerificationCode(): This method will send the verification code. You can also see that I am concatenating +91 with the mobile number which is the country code for India. But in real scenario you might have users from different countries. So if that is the case, you need to let the user select their country as well so that you know the code, and you can also  tell the user to input the number with country code.
  • mCallbacks: This is our callback that will help us to know the code is sent or not. It has three methods.
    • onCodeSent(): This is called when the code is sent successfully.  The first parameter here is our verification id that is sent. So we are storing it in our mVerificationId object.
    • onVerificationFailed(): This method is called when the verification is failed for some reasons, so here we are only displaying a simple toast.
    • onVerificationCompleted(): This method is called when the verification is completed. Here we have the PhoneAuthCredential Object which will give us the code if it is automatically detected.

Verifying Code and Sign In

  • To verify verification code we will use this method. If the verification is successful we will let the user sign in into the application.

  • Thats it, it will work. Now let me also show you the full code for

VerifyPhoneActivity Code

  • Here is the full code of I have also written comments to explain. But if you have any confusion, you can leave your comment below.

  • Thats it now you can try running your application.

Firebase Phone Authentication Source Code

  • Still having troubles? Don’t worry you can get my source code. Just unlock the link by subscribing to my youtube channel.

Firebase Phone Authentication Tutorial Source Code

So that’s all for this Firebase Phone Authentication Tutorial friends. I hope you found it useful, and if you really did then please help me by sharing this post will all your friends who are learning Android Application Development.

And for any question you can leave your comments here. Thank You 🙂


41 thoughts on “Firebase Phone Authentication Android Tutorial”

  1. hi bilal,
    this is abhishek kumar. your all post are awesome we learn lot.
    just to inform users to there is very best to get smscode by phoneauth and no need to use others library.
    example and uses –
    onverificationcomplete –


    and set to otp edittext.
    this is work like charm and no other coding overhead.
    note: when autoretrievel from old sms or play service that time u need to handle it..

    • Go to firebase console and in authentication search mobile number and right click to remove from users.
      hope this will help.

  2. sir coding is working fine but otp is not generated…..the progress bar keeps on rotating but could not fetch the otp ….could you please suggest me a solution to this problem

  3. very simple and nice code worked very well. there were few error like snackbar throws runtime error. and every time it ask to login whereas only it should ask for first time and next time it just need to check whether user is there or not and should login automatically… thanks
    and for those whose code doesn’t work kindly goto firebase console and enable phone authentication from dashboard and download json providing your sha-1 key and put it into your project. it will work

  4. sir coding is working fine but otp is not generated…..the progress bar keeps on rotating but could not fetch the otp ….could you please suggest me a solution to this problem. please its urgent

  5. hi
    my self ashish kumar , your all post are awesome we learn lot. thank you for this
    i want to some suggession to dovelep some app that help for getting job
    plz help me

  6. Hello,
    I use this code its working perfectly on my real device. THANK YOU SO MUCH
    But When I generate signed APK for app release purpose than its shows error. I already put SHA and authenticated phone verification in firebase. please reply me ASAP.
    ERROR: this app is not authorized to use firebase authentication please verify that the correct package name and sha-1 are configured in firebase console

  7. Hi Belal, it worked well but when i tried uninstalling and retesting the otp never came . Is it one time only? I tried deleting.. uninstalling . . Still didnt work. Please help.


  8. But every time you have to sign in, how do you sign in once?
    When the application is closed and restarted again, it is confirmed that if the activation is activated, the application will be immediately added to the application
    If the activation is not activated, the login will be activated

  9. How do I make sure that the number is already activated?
    As well as how to make the application work on the profile without logging in after it has already been activated
    Any activation is done only once

  10. This is really an appreciable tutorial but if the user no is already registered then also it is sending the otp.
    so how we create method, so that if the user is already registered so it will show a toast message.

  11. Thanks for this code. Code work’s properly. But there is one issue when the user is exiting from the application the user is getting automatically logged out and because of this during next login the application is requesting for login details to be re enter.
    please suggest code for is problem……..

  12. The acceptation here is ones the user is logged in from next time onward the application should no request to re-enter the login details..

  13. Hy bilal.its a good tutorial, and i am beginner to android ..but while coding we get some error..and can u rewrite or reconstruct ur code by defining resending the code again….and something which are missing on the code..its will be very helpfull to us..many are getting prblm pls solve it.

  14. Hello Sir Belal.,
    First off all thanks for the tutorial.
    I have some issue when it comes to put sendVerificationcode(mobile) under String mobile.. The app launch the first activity but suddenly close without going to VeryPhone activity..
    Can you help me


  15. It is not working. Even it is not generating the otp to the respective numbers. And also getting crashed after clicking on the continue button. Kindly help me out or provide any accurate sourse codes. Please!

  16. Hello,
    I use this code, but call void “public void onVerificationFailed(FirebaseException e) {
    Log.e(“onVerificationFailed”, e.getMessage());
    if (e instanceof FirebaseAuthInvalidCredentialsException) {
    // Invalid request
    Log.e(“InvalidRequest”, e.getMessage());
    } else if (e instanceof FirebaseTooManyRequestsException) {
    // The SMS quota for the project has been exceeded
    Toast.makeText(VerifyPhoneActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    I has get error: log cat “E/onVerificationFailed: The given sign-in provider is disabled for this Firebase project. Enable it in the Firebase console, under the sign-in method tab of the Auth section”
    I was enabled the Phone Number sign-in method in the Firebase console, the Authentication section of my project
    How to fix it

  17. It is not working. When I run test on my mobile device got error in method : onVerificationFailed: The given sign-in provider is disabled for this Firebase project. Enable it in the Firebase console, under the sign-in method tab of the Auth section.
    Although I certainly was enabled phone in the Firebase console. Why?

  18. Hello Belal,
    your code is working perfect at API 25 and older. But it does not work at Oreo and another new Version. Can you please help to solve this problem?


Leave a Comment