Hey guys...
In this blog, we are going to see how to send OTP in Andriod app. We will make an app where we will input the mobile number the OTP will come to that mobile number.
Now its time to make the close of this blog thank you for reading this blog, if you are getting any problem comment us.
THANK YOU.
In this blog, we are going to see how to send OTP in Andriod app. We will make an app where we will input the mobile number the OTP will come to that mobile number.
How to send OTP in Android App?
We will send OTP via the TextLocal API. For this, we need the API of TextLocal.
Steps to get API from TextLocal.
Go and register with TextLocal. Then you will get 10 credits free to test the SMS service.
Then go to the Setting section under setting click create a new API key and after crating copy that API and do the coding.
If you need more Credits!!
If you want more credits then you need to buy with some price. Then there are some procedures to create sender name for your particular business name showing over the sender name, the support team will guide you.
If you have brought the credits then you have to create templates to send OTP or any messages. After requesting the template the team will approve it. If won't unable to create a template then contact to the support team they will guide you.
After creating the template you have to copy that same template in the coding part.
What we will build?
![]() |
Enter OTP |
![]() |
Verify OTP |
Steps to create this !!!
Step 1. Create a Project and Add The Internet Permissions
Add the internet permission to our project in AndroidManifest.xml file.
Step 2. Create The XML file or Design
I have taken one root LinearLayout and two child LineaeLayouts. You can create the design as your need.
Code: activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="20dp" | |
> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:gravity="center" | |
android:padding="20dp" | |
> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Enter your mobile number." | |
android:textSize="25sp" | |
android:textStyle="bold" | |
android:textAlignment="center" | |
/> | |
<EditText | |
android:id="@+id/enterPhone" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="8dp" | |
android:layout_marginEnd="8dp" | |
android:layout_marginBottom="8dp" | |
android:ems="10" | |
android:inputType="phone" | |
android:layout_marginTop="20dp" | |
android:hint="Enter mobile number" | |
android:padding="15dp" | |
/> | |
<Button | |
android:id="@+id/sendOtp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="20dp" | |
android:padding="20dp" | |
android:text="Send OTP" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:gravity="center" | |
android:padding="20dp" | |
android:id="@+id/verify_section" | |
android:layout_marginTop="20dp" | |
> | |
<TextView | |
android:id="@+id/textView1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Verify the Otp!!" | |
android:textSize="25sp" | |
android:textAlignment="center" | |
android:textStyle="bold" | |
/> | |
<EditText | |
android:id="@+id/enterOtp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="8dp" | |
android:layout_marginEnd="8dp" | |
android:layout_marginBottom="8dp" | |
android:ems="10" | |
android:padding="15dp" | |
android:layout_marginTop="20dp" | |
android:inputType="number" | |
android:hint="Enter otp here" | |
/> | |
<Button | |
android:id="@+id/verifyOtp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Verify Otp" | |
android:padding="20dp" | |
android:layout_marginTop="20dp" | |
/> | |
</LinearLayout> | |
<TextView | |
android:layout_width="match_parent" | |
android:id="@+id/show_result" | |
android:layout_height="wrap_content" | |
android:textStyle="bold" | |
android:textSize="20sp" | |
android:textColor="#CA0D0D" | |
android:textAlignment="center" | |
/> | |
</LinearLayout> |
Step 3. Create The Functionality in JAVA
I have mentioned the comments in each line of code please follow the comments to understand, if you are getting any problems, comment on the page below.
Code: MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.sendotp; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.StrictMode; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity { | |
// Button used to send otp | |
Button send_otp_btn; | |
// Button to verify the otp | |
Button verify_otp_btn; | |
// This is used to store the randow no or otp that will be sent to the user | |
int randomNumber; | |
// These two are our editexts | |
EditText enter_mobile_no_ediText; | |
EditText enter_otp_editText; | |
// phone_no used to store the phone no from edittext | |
String phone_no; | |
// get_otp_from_editText used to store the entered otp from verify section | |
String get_otp_from_editText; | |
// This will sent to the user, the random no | |
String random_no_we_sent; | |
// This textview used to show the output | |
TextView show_result; | |
// This layout holds the verify section used to set visible and invisible property | |
LinearLayout verify_section; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Used to prevent the Os.NetworkThreadException | |
StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build(); | |
StrictMode.setThreadPolicy(policy); | |
// Link with our edittext enter mobile no | |
enter_mobile_no_ediText = findViewById(R.id.enterPhone); | |
// button used to submit the phone no | |
send_otp_btn = findViewById(R.id.sendOtp); | |
// Link with our edittext enter otp to verify | |
enter_otp_editText = findViewById(R.id.enterOtp); | |
// button used to submit the otp | |
verify_otp_btn = findViewById(R.id.verifyOtp); | |
// Text View used to show the output | |
show_result= findViewById(R.id.show_result); | |
// The otp verify page | |
verify_section = findViewById(R.id.verify_section); | |
// Initially we set to invisible after getting the otp we will again set visible | |
verify_section.setVisibility(View.INVISIBLE); | |
// Here we set the listener to the send otp button | |
send_otp_btn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
//Here we declared a method to do the send otp stuffs to make the code simpler | |
// Below we have initialised this method | |
getOtp(); | |
} | |
}); | |
// Here we set the listener to the verify otp button | |
verify_otp_btn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
//Here we declared a method to verify the otp stuffs to make the code simpler | |
// Below we have initialised this method | |
verifyOTP(); | |
} | |
}); | |
} | |
private void getOtp() { | |
try { | |
// Get the data from the mobile no textfield | |
phone_no = enter_mobile_no_ediText.getText().toString(); | |
// To show our mobile no is coming or not | |
Toast.makeText(this, phone_no, Toast.LENGTH_SHORT).show(); | |
// SET THE API FROM THE TEXTLOCAL | |
String apiKey = "apikey=" + "YOUR API"; | |
// GENERATE THE RANDOM NUMBER TO SEND THE OTP | |
Random random= new Random(); | |
randomNumber=random.nextInt(999999); | |
// ENTER THE MESSAGE AND SET THE CONCATENATE THE RANDOM VARIABLE | |
// IF YOU HAVE CREATED A TEMPLATE THEN PASTE THE CORRECT TEMPLATE | |
String message = "Hey, thanks for trying SMART WORK, your OTP is "+randomNumber; | |
// ENTER YOUR SENDER NAME | |
// IF YOU DONT HAVE THE SENDER NAME DEFAULT IS TXTLCL | |
String sender = "&sender=" + "TXTLCL"; | |
// HERE WE ARE ATTACHING THE MOBILE NO THAT IS COMING FROM THE EDITTEXT | |
String numbers = "&numbers=" +phone_no; | |
// HERE WE ARE MAKING THE REQUEST TO THE URL | |
// WE HAVE ATTACHED THE PROPER USL WITH API KEY, SENDER NAME, NUMBER AND MESSAGE FROM THE ABOVE VARIABLE | |
String data = "https://api.textlocal.in/send/?apikey="+apiKey+"1&sender="+sender+"&numbers="+numbers+"&message="+message; | |
URL url = new URL(data); | |
URLConnection conn = url.openConnection(); | |
conn.setDoOutput(true); | |
// Get the response | |
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String line; | |
String sResult=""; | |
while ((line = rd.readLine()) != null) { | |
// Process line... | |
sResult=sResult+line+" "; | |
} | |
rd.close(); | |
// STORING THE RANDOM NO TO NEXT VERIFY | |
random_no_we_sent = String.valueOf(randomNumber); | |
// SHOWING TOAST THAT IS Otp Sent Successfully" | |
Toast.makeText(this, "Otp Sent Successfully", Toast.LENGTH_SHORT).show(); | |
// AFTER SENDING THE OTP | |
verify_section.setVisibility(View.VISIBLE); | |
} catch (Exception e) { | |
// HERE WE SHOWN THE ERROR | |
System.out.println("Error SMS "+e); | |
Toast.makeText(this,"Error "+ e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
// HERE WE HAVE VERIFIED THE OTP | |
private void verifyOTP() { | |
// GET THE OTP FROM THE TEXTFIELD | |
get_otp_from_editText = enter_otp_editText.getText().toString(); | |
// THEN CHECK THE RANDOM NUMBER WITH OTP FROM EDITTEXT | |
if(random_no_we_sent.equals(get_otp_from_editText)) { | |
show_result.setText("Otp is verified."); | |
} else { | |
// SHOW THE OUTPUT | |
show_result.setText("NOT VERIFIED"); | |
} | |
} | |
} | |
Now its time to make the close of this blog thank you for reading this blog, if you are getting any problem comment us.
THANK YOU.
Every slot machine evaluation we publish features a section dedicated to the payout proportion with easy-to-understand data about the video games with frequent payouts and whether one is a loose slot or not. Many on line casino strategies inform you to stay away from each in style slot sport. 스마일토토 Those, they are saying, are the video games with extra severe|the extra serious} payout proportion among all those on the on line casino ground. That's as a result of|as a outcome of} though have the ability to|you probably can}'t win at slot machines each time, choosing a great slot machine is finest way|one of the only ways|the best way} to enhance your odds and let the RTP of the on line casino slot do the ‘hard work' for you. Knowing how to to|tips on how to} pick a slot machine wants more than you guessing when a slot machine will hit. The finest slot machine to play is the one which comes with the right mixture of volatility, return to player, limits, and on line casino bonus.
ReplyDelete