Android Javascript Ajax Html PHP Example

Mobile number verification Android through OTP


In this example we will discuss How verify mobile number in android.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

<EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="30dp"
    android:hint="Enter your device phone number"
    android:id="@+id/phoneNumber"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Send Otp"
        android:id="@+id/sendOtpBtn"/>

</LinearLayout>

activity_otpverification.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OTPVerificationActivity"
    android:orientation="vertical">



    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="30dp"
        android:hint="Enter your OTP"
        android:id="@+id/otpNumber"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Submit"
        android:id="@+id/submitBtn"/>


    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center"
        android:textStyle="bold"
        android:padding="20dp"
        android:id="@+id/successMsg"/>
</LinearLayout>

MainActivity.java


package com.example.otpverificationdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText phoneNumber;
    private Button sendOtpBtn;
    char[] otp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phoneNumber = findViewById(R.id.phoneNumber);
        sendOtpBtn = findViewById(R.id.sendOtpBtn);

        sendOtpBtn.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        Random random = new Random();


        //Generate random integer in range 0 to 9

       otp = new char[4];
        for (int i=0; i<4; i++)
        {
            otp[i]= (char)(random.nextInt(10)+48);
        }

//        Toast.makeText(getApplicationContext(), String.valueOf(otp), Toast.LENGTH_SHORT).show();

        String number  = phoneNumber.getText().toString();

       if(!(phoneNumber.getText().toString().equals(""))) {
           new MyAsyncTask().execute("https://api.msg91.com/api/sendhttp.php?route=4&sender=TESTIN&message=OTP for your OTP Verification App is : "+String.valueOf(otp)+"&country=91&flash=&unicode=&mobiles="+number+"&authkey=297116AFCGQdLuvdm25d96f3f7");
       }else{
           phoneNumber.setError("Please Enter your mobile number");
       }


    }

    public class MyAsyncTask extends AsyncTask{
     URL Url;
     HttpURLConnection httpURLConnection = null;
     InputStream inputStream;


        @Override
        protected Void doInBackground(String... urls) {

            try {
                Url = new URL(urls[0]);
                httpURLConnection = (HttpURLConnection)Url.openConnection();
                inputStream = httpURLConnection.getInputStream();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            Intent intent = new Intent(MainActivity.this, OTPVerificationActivity.class);
            intent.putExtra("otp_number",String.valueOf(otp));
            startActivity(intent);
        }
    }
}

OTPVerificationActivity.java


package com.example.otpverificationdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class OTPVerificationActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText otpNumber;
    private Button submitBtn;
    private TextView successMsg;
    String otpValue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otpverification);

        otpNumber = findViewById(R.id.otpNumber);
        submitBtn = findViewById(R.id.submitBtn);
        successMsg = findViewById(R.id.successMsg);



        Intent intent = getIntent();
        otpValue = intent.getStringExtra("otp_number");



        otpNumber.setVisibility(View.VISIBLE);
        submitBtn.setVisibility(View.VISIBLE);

        submitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        boolean checkOtpNumber = TextUtils.isEmpty(otpNumber.getText().toString());

        if(checkOtpNumber == false){

            if(otpValue.equals(otpNumber.getText().toString())){

                otpNumber.setVisibility(View.INVISIBLE);
                submitBtn.setVisibility(View.INVISIBLE);


                successMsg.setText("Successfully Verified!");

            }else{

                successMsg.setText("OTP doesn't match! Please Enter again!");


            }
        }else{
            otpNumber.setError("Please Enter OTP First!");
        }
    }
}