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 …

android sms verification app

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 0 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

[sociallocker id=1372] Android SMS Verification Application[/sociallocker]

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x