Android SMS Verification App – Phone Verification with OTP

Hello friends, I got a lots of emails and facebook requests to post a tutorial for phone number verification. Some people also asked me for SMS verification code for android. So in this tutorial we will create an Android SMS Verification App.

You already seen this example in some popular applications like WhatsApp, Hike etc. They verify the user’s phone number by sending an OTP (One Time Password) to the phone number registered.

To avoid spamming this is the best way to confirm your user with their phone number. And that is why verifying user’s with their phone number is one of the most common feature for your android app now days.

So in this tutorial we will create an Android SMS Verification App for registered users.

Android SMS Verification App – Video

You can check this video to know what we will be creating in this Android SMS Verification tutorial.

Creating our SMS API

  • To verify the user’s phone number we will send the verification code via SMS. For sending SMS we need an SMS API.
  • SMS’s are of two types
    • Promotional: It will not be sent to DND activated phone numbers. So for OTP  or Phone verification we cannot use promotional sms.
    • Transactional: It will be sent to all numbers, doesn’t matter a number has activated DND or not. In this tutorial I am using Transactional sms.
  • The bad news is for you guys is I haven’t found any provider who is providing SMS API for free. This tutorial is sponsored by Cheap SMS Bazaar. They gave me some free transactional sms credits to write this tutorial.
  • There are a lot of SMS API provider’s you can google for them. You can also buy it from Cheap SMS Bazaar. If you will signup to Cheap SMS Bazaar, they would give you 10 free promotional sms. You can test this tutorial with promotional sms as well. But with promotional sms your sms will not be sent to DND activated numbers.
  • I am assuming that you all are using Cheap SMS Bazaar. So go to your dashboard. And then go to API Documents. I am using the transactional one and my API link is.

http://login.cheapsmsbazaar.com/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898xxxxxx&sid=SenderId&msg=test%20message&fl=0&gwid=2

user: Your login username.
password: Your login password.
msisdn: Single mobile number or multiple mobile numbers separated by comma(10 digits or +91).
sid: Approved sender id(Only 6 characters).
msg: Your message content(Minimum 459 characters/3 messages).
fl: if flash message then 1 or else 0
gwid: 2 (its for Transactions route.)
Note: Only 100 mobile numbers are allowed.

  • We will send the sms to only a single mobile number.
  • We will get the following JSON in response.

Creating Database and Scripts for Android SMS Verification Application

  • Go to phpMyAdmin and create the following table. (I am using Hostinger’s free hosting).
mysql database
MySql Database
  • As you can see I have 6 columns. ID is PRIMARY KEY and AUTOINCREMENT Username and Phone is set to UNIQUE. verified is TINY INT and will store only two values 0 and 1. (o means not verified and 1 means verified). The default value for verified is set to i.e. for every row the default value is unverified. And when user will enter the correct verification code we will change it to 1 i.e. verified.
  • Go to your server’s directory and create a folder for this project. (I created AndroidOTP)
  • We need to connect to the database first, so create a script dbConnect.php and write the following code.

  • Now to handle the registration request create a file register.php. This file would store the user detail to database. It will also send the OTP or Verification Code via SMS.

  • After sending the OTP to user’s mobile number we need to confirm the OTP from user. And for this we need one more php file. Create a new file confirm.php and write the following code.

  • Thats it for the server side part. Note the URL for the both the files register.php and confirm.php. And lets create an Android Project.

Android SMS Verification Project

  • Open Android Studio and create a new project and add the following dependencies that will be used.

  • I have added volley because in this android sms verification tutorial I am using volley for the network operations. And for the layout design I have added design:23.0.0.
  • Now in your project create a java class to store the important constants that will be used in our app. I created Config.java

  • Now we need to design a registration activity. I have created the following layout for this android sms verification.
android sms verification
Android SMS Verification
  • For creating the above layout you can use the following code.

  • After user will tap the REGISTER button, a dialog box will appear to enter the confirmation code sent. For this dialog box we need to create a layout resource file. Create dialog_confirm.xml file and write the following code.

  • Now come to MainActivity.java. 

  • On success we are starting a new activity that is not yet created. So create a new activity named success. And write the following xml code for the layout file.

  • The above code will produce the following layout.
android sms verification
Android SMS Verification
  • For the java file of this activity (Success.java) I haven’t done anything.

  • At last add internet permission to your manifest file.

  • Now run your application.
android sms verification
Android SMS Verification
  • Bingo! Its working absolutely fine. You can get the project from GitHub Repository. Just go to the link given below.

Get Source Code of this Android SMS Verification App from GitHub

Some More Android Application Development Tutorial You Must Check 

So thats all for this Android SMS Verification Tutorial. Please support by giving your feedbacks with your comments. And don’t hesitate to ask if having queries or confusions regarding this Android SMS Verification Tutorial. Thank You 🙂

112 thoughts on “Android SMS Verification App – Phone Verification with OTP”

  1. Thanks belal ,simplified coding is a really very good website for learning…………there is no other website where coding as simpler as simplified coding provides is available…….
    Thanks Once again……………
    Hope today i spelled u correctly

    Reply
    • on your above comment i think u done programming can u please help me …i got

      Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user ‘root’@’localhost’ (using password: YES) in G:\wamp\www\android_sms\include\DbConnect.php on line 24
      Failed to connect to MySQL: Access denied for user ‘root’@’localhost’ (using password: YES)

      i got this error in adatabase can u help me how i solve this and please provide me your coding u add the programme

      please reply and help me

      Reply
  2. Hey hi after i am running this code i am getting the error like this

    :[ Error:Execution failed for task ‘:app:transformClassesWithJarMergingForDebug’.
    > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/http/message/BasicHeaderElementIterator.class ]..

    How to clear this error ?

    Reply
  3. I developed the app using Eclipse and also imported the volley.jar file successfully. But everytime I launch the app it unfortunately stops. I tried to figure out the problem then found that in MainActivity.java the line ” requestQueue = Volley.newRequestQueue(this);” is the reason. Removing this line makes the app load the main layout but could not switch to other when I press the Register button. Waiting eagerly for your reply.

    Reply
  4. Can u send me same code in python language that send otp no to mobile via sms for verification of mobile no actually i have created some parts but its not working plsease help.
    My email id is riteshseth92@yahoo.com.
    Thank You
    Thanks in advance.

    Reply
  5. I have some problems. Why if i click the register button, that show me “Username or password already registered?”. I had create the php file server and database like your screenshot and set the username and phone as unique. Please give me the solution. Thanks anyway.

    Reply
  6. hey, i need some help for my website. i am not making a android app, but a client side for my website. do u know coding for client side? where we will capture details and verify otp success message?

    Reply
  7. I am getting error when i hit the register button it shows “Username or Phone number already registered” nothing happend. then i checked my database its empty no datas are stored in my database…

    Please advice me ASAP

    Reply
  8. Value of type java.lang.String cannot be converted to JSONObject these type of error coming when try to execute your project.

    Downloaded from Github

    Reply
  9. when im press regsitration button, its processed and copme to one error, that is..
    1. ” onResponse(Register.java:165)”

    165 line code : JSONObject jsonResponse = new JSONObject(response);

    2. onResponse(Register.java:159)

    159 line code : new Response.Listener()

    how to slove

    3. i cant import a these :

    import com.android.volley.NetworkResponse;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.StringRequest;

    Reply
  10. i am not getting dialog box to confirm the code after Registered. m using msg91 for otp bt m nt getting dialog box.. plz reply me soon…

    Reply
  11. Hi belal i am follow your tutorial to send otp on the mobile device but the api not response properly. please help me to resolve this problem

    Reply
  12. I have some problems. Why if i click the register button, that show me “Username or password already registered?”. I had create the php file server and database like your screenshot and set the username and phone as unique. Please give me the solution. Thanks anyway.

    Reply
  13. hello sir,
    i’ve designed a contacts page in studio….which contains two fields Name and contact No. and a button to submit the details…when i click on the Submit button a message has to be sent to the mentioned mobile number

    how to do it by using URL???

    Hope i’ll get reply from ur side soon

    Thanks in advance

    Reply
  14. Thank you for posting. but i want to send otp in mobile without save otp in database then what can i do..?

    please reply me ASAP..

    Thanks in Advance.

    Reply
  15. Value <br of type java.lang.String cannot be converted to JSONObject
    Getting this type of Error when posting data

    Reply
  16. Getting this type of Error when posting data
    Value <br of type java.lang.String cannot be converted to JSONObject
    and also i cant download this source code….Please help me to resolve this problem

    Reply
  17. Hello dear
    First of all thanks to you for this great tutorial. I am successfully send promotional sms on my device through codes and api provided by you. But the problem is alert dialog is not showing on the screen. How can confirm without entering otp. Please help.

    Reply
  18. Sir Actually i am confusing about {
    “ErrorCode”:”000″,
    “ErrorMessage”:”Success”,
    “JobId”:”381a8090-b230-42fa-ac04-157cc2142bfa”,
    “MessageData”:[
    {
    “MobileNumber”:”919898xxxxxx “,
    “MessageParts”:[
    {
    “MessageId”:”919898xxxxxx -67e3765cdf034f438a432eacb88d0c14″,
    “MessagePartId”:1,
    “MessageText”:”test message”
    }
    ]
    }
    ]
    }

    what will be name and dot extension name of that file

    Reply
  19. hii,Belal
    I m using ur post but i get one error when i m register then i m get otp but i didnt call confirmOtp(); function

    try {
    //Creating the json object from the response
    JSONObject jsonResponse = new JSONObject(response);
    // confirmOtp();
    //If it is success
    if(jsonResponse.getString(Config.TAG_RESPONSE).equalsIgnoreCase(“Success”)){
    confirmOtp();

    //Asking user to confirm otp

    }else{
    //If not successful user may already have registered
    Toast.makeText(MainActivity.this, “Username or Phone number already registered”, Toast.LENGTH_LONG).show();
    }
    } catch (JSONException e) {
    e.printStackTrace();
    }
    }
    },

    ” if(jsonResponse.getString(Config.TAG_RESPONSE).equalsIgnoreCase(“Success”)){
    confirmOtp();”

    this code is not getting execute when I run this tutorial

    Reply
  20. jsonResponse.getString(Config.TAG_RESPONSE).equalsIgnoreCase(“Success”) this condition is not getting executing when I am implementing this and directly display toast msg as “Username or Phone number already registered”

    Reply
  21. {“ErrorCode”:”15″,”ErrorMessage”:”Invalid SenderId”,”JobId”:null,”MessageData”:null}

    I am getting this error
    What is the sender id which i have to put in api string?

    Reply
  22. Hi Belal
    Am getting a fatal exception error
    “Unable to start activity ComponentInfo {… / … MainActivity} android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.TextInputLayout ”
    What could be the issue?

    Reply
  23. Hi belal I have to fill this or not

    user: Your login username.
    password: Your login password.
    msisdn: Single mobile number or multiple mobile numbers separated by comma(10 digits or +91).
    sid: Approved sender id(Only 6 characters).
    msg: Your message content(Minimum 459 characters/3 messages).
    fl: if flash message then 1 or else 0
    gwid: 2 (its for Transactions route.)

    Reply
  24. Hi Belal,
    1. How to get sender id in cheap sms bazaar.
    2. Weather we want to fill our username,password and sender id in register.php .

    Reply
  25. hi belal
    plz check karo and reply do
    in this project
    I m using ur post but i get one error when i m register then i m get otp but i didnt call confirmOtp(); function
    cannot call
    there is an error
    Value 1323851374 of type java.lang.Inger cannot converted to JSONObject

    plz reply
    or else any can solve this type of problem can share with us plz

    Reply
  26. Hello Sir, I’m not able to verify the otp. It is saving the details in my database. it is also sending an otp but the verification screen is unable to open and it is saying that user already exists. Please help me with this.

    Reply
  27. {“ErrorCode”:”23″,”ErrorMessage”:”Parameter missing”,”JobId”:null,”MessageData”:null} i got this please help and after how many days sender id is approved

    Reply
  28. hello sir can u please provide this coding on video .When i run the programme it show error and i can’t understand because i do this first time please sir provide me on video and above u create a table in database i also do but it show error please sir solve this ….give answer as soon as possible.

    Reply
  29. ample.user.smsverify E/SmsActivity: Posting params: {email=t@dcom, name=jay, mobile=8745216699}
    11-14 06:07:07.353 2842-3029/com.example.user.smsverify E/SmsActivity: Posting params: {email=t@dcom, name=jay, mobile=8745216699
    I got error when i run the programme how i solve this

    Reply
  30. Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user ‘root’@’localhost’ (using password: YES) in G:\wamp\www\android_sms\include\DbConnect.php on line 24
    Failed to connect to MySQL: Access denied for user ‘root’@’localhost’ (using password: YES)

    i got this error in database how i solve this and please give me answer please sir help me to solve this error and reply sonner thanku

    Reply
  31. Hi Belal,
    Great tutorial, the problem i have is to find a free SMS service where i can use and test this code. Wondering, is it possible to integrate this with Google Voice, i only want to test in US and i have free google voice account. Is it possible to test with google voice ?

    Reply
  32. How can I implement automatic verification in just like whatsapp, as soon as the message code arrives, instead of the user typing the code by hand?

    Reply
  33. you created confirmotp() method but you make clicklistener inside confirmOtp() method and call confirmOtp Method inside confirmOtp() method.????????????

    Reply
  34. hai belal am suba .. i presently working as app developer . your tutorials are very useful for me . thank you belal

    Reply
  35. Sir, I didn’t understood what you mean by “Only 100 mobile numbers are allowed.”
    please reply soon……..

    Reply
  36. Bro I want to do Android SMS Verification App – Phone Verification with OTP with FIREBASE Database. Actually i did some activity using FIREBASE so after dat i want to do the ” Android SMS Verification App – Phone Verification with OTP with FIREBASE Database” so plz help me how to do dis one…..tq bro

    Reply
  37. Sir Ye jo JSON RESPONSE HE use PHP script me kha par mention krte he ,kunki meri activity ke ander Register vale progressing bar ke baad OTP confirm vala layout open nahi ho rha please give your suggestion,

    Reply
  38. hi belal, data is reached in database comptly but I am not getting OTP in alertdiolog and notification also .what is problem can u pls tell me

    Reply
  39. Can anyone explain me its use and when and how to create this file and get excess

    {
    “ErrorCode”:”000″,
    “ErrorMessage”:”Success”,
    “JobId”:”381a8090-b230-42fa-ac04-157cc2142bfa”,
    “MessageData”:[
    {
    “MobileNumber”:”919898xxxxxx “,
    “MessageParts”:[
    {
    “MessageId”:”919898xxxxxx -67e3765cdf034f438a432eacb88d0c14″,
    “MessagePartId”:1,
    “MessageText”:”test message”
    }
    ]
    }
    ]
    }

    Reply

Leave a Comment