in build.gradle(:app)


// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:30.4.1')

// Add the dependency for the Firebase Authentication library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-auth'

in OtpSendActivity.java


public class OtpSendActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

Button btnSend;
EditText etPhone;
ProgressBar progressBar;

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

mAuth = FirebaseAuth.getInstance();

btnSend = findViewById(R.id.btnSend);
etPhone = findViewById(R.id.etPhone);
progressBar = findViewById(R.id.progressBar);

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etPhone.getText().toString().trim().isEmpty()) {
Toast.makeText(OtpSendActivity.this, "Invalid Phone Number", Toast.LENGTH_SHORT).show();
} else if (etPhone.getText().toString().trim().length() != 10) {
Toast.makeText(OtpSendActivity.this, "Type valid Phone Number", Toast.LENGTH_SHORT).show();
} else {
otpSend();
}
}
});
}

private void otpSend() {
progressBar.setVisibility(View.VISIBLE);
btnSend.setVisibility(View.INVISIBLE);

mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {

}

@Override
public void onVerificationFailed(FirebaseException e) {
progressBar.setVisibility(View.GONE);
btnSend.setVisibility(View.VISIBLE);
Toast.makeText(OtpSendActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}

@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken token) {
progressBar.setVisibility(View.GONE);
btnSend.setVisibility(View.VISIBLE);
Toast.makeText(OtpSendActivity.this, "OTP is successfully send.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(OtpSendActivity.this, OtpVerifyActivity.class);
intent.putExtra("phone", etPhone.getText().toString().trim());
intent.putExtra("verificationId", verificationId);
startActivity(intent);
finish();
}
};

PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber("+94" + etPhone.getText().toString().trim())
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(mCallbacks)
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}

}

in activity_otp_send.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".OtpSendActivity">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="70dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_call" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="OTP Verification"
android:textColor="@color/black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="We will send otp to your entered mobile number."
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:background="@drawable/border_left"
android:padding="15dp"
android:text="+94"
android:textSize="20sp"
app:layout_constraintEnd_toStartOf="@+id/etPhone"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<EditText
android:id="@+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:background="@drawable/border_right"
android:ems="10"
android:hint="1234567890"
android:inputType="phone"
android:padding="15dp"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginBottom="45dp"
android:text="SEND"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etPhone" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/btnSend"
app:layout_constraintEnd_toEndOf="@+id/btnSend"
app:layout_constraintStart_toStartOf="@+id/btnSend"
app:layout_constraintTop_toTopOf="@+id/btnSend" />
</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

in OtpVerifyActivity.java


private String verificationId;

TextView tvMobile, tvResendBtn;
Button btnVerify;
ProgressBar progressBarVerify;
EditText etC1, etC2, etC3, etC4, etC5, etC6;

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

tvMobile = findViewById(R.id.tvMobile);
tvResendBtn = findViewById(R.id.tvResendBtn);
btnVerify = findViewById(R.id.btnVerify);
progressBarVerify = findViewById(R.id.progressBarVerify);
etC1 = findViewById(R.id.etC1);
etC2 = findViewById(R.id.etC2);
etC3 = findViewById(R.id.etC3);
etC4 = findViewById(R.id.etC4);
etC5 = findViewById(R.id.etC5);
etC6 = findViewById(R.id.etC6);

editTextInput();

tvMobile.setText(String.format(
"+94-%s", getIntent().getStringExtra("phone")
));

verificationId = getIntent().getStringExtra("verificationId");

tvResendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(OtpVerifyActivity.this, "OTP Send Successfully.", Toast.LENGTH_SHORT).show();
}
});

btnVerify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBarVerify.setVisibility(View.VISIBLE);
btnVerify.setVisibility(View.INVISIBLE);
if (etC1.getText().toString().trim().isEmpty() ||
etC2.getText().toString().trim().isEmpty() ||
etC3.getText().toString().trim().isEmpty() ||
etC4.getText().toString().trim().isEmpty() ||
etC5.getText().toString().trim().isEmpty() ||
etC6.getText().toString().trim().isEmpty()) {
Toast.makeText(OtpVerifyActivity.this, "OTP is not Valid!", Toast.LENGTH_SHORT).show();
} else {
if (verificationId != null) {
String code = etC1.getText().toString().trim() +
etC2.getText().toString().trim() +
etC3.getText().toString().trim() +
etC4.getText().toString().trim() +
etC5.getText().toString().trim() +
etC6.getText().toString().trim();

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
FirebaseAuth
.getInstance()
.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull @NotNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progressBarVerify.setVisibility(View.VISIBLE);
btnVerify.setVisibility(View.INVISIBLE);
Toast.makeText(OtpVerifyActivity.this, "Welcome...", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(OtpVerifyActivity.this, ProductActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
} else {
progressBarVerify.setVisibility(View.GONE);
btnVerify.setVisibility(View.VISIBLE);
Toast.makeText(OtpVerifyActivity.this, "OTP is not Valid!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
});
}

private void editTextInput() {
etC1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
etC2.requestFocus();
}

@Override
public void afterTextChanged(Editable s) {

}
});
etC2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
etC3.requestFocus();
}

@Override
public void afterTextChanged(Editable s) {

}
});
etC3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
etC4.requestFocus();
}

@Override
public void afterTextChanged(Editable s) {

}
});
etC4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
etC5.requestFocus();
}

@Override
public void afterTextChanged(Editable s) {

}
});
etC5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
etC6.requestFocus();
}

@Override
public void afterTextChanged(Editable s) {

}
});
}
}

in activity_otp_verify.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".OtpVerifyActivity">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="70dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_otp" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Verify OTP"
android:textColor="@color/black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Enter your received OTP here"
app:layout_constraintEnd_toEndOf="@+id/textView4"
app:layout_constraintStart_toStartOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/textView4" />

<TextView
android:id="@+id/tvMobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="+91-1234567890"
app:layout_constraintEnd_toEndOf="@+id/textView4"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/textView5" />

<EditText
android:id="@+id/etC1"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="3dp"
android:background="@drawable/border"
android:ems="10"
android:gravity="center"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintEnd_toStartOf="@+id/etC2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvMobile" />

<EditText
android:id="@+id/etC2"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="3dp"
android:background="@drawable/border"
android:ems="10"
android:gravity="center"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/etC1"
app:layout_constraintEnd_toStartOf="@+id/etC3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/etC1"
app:layout_constraintTop_toTopOf="@+id/etC1" />

<EditText
android:id="@+id/etC3"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="3dp"
android:background="@drawable/border"
android:ems="10"
android:gravity="center"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/etC1"
app:layout_constraintEnd_toStartOf="@+id/etC4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/etC2"
app:layout_constraintTop_toTopOf="@+id/etC1" />

<EditText
android:id="@+id/etC4"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="3dp"
android:background="@drawable/border"
android:ems="10"
android:gravity="center"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/etC1"
app:layout_constraintEnd_toStartOf="@+id/etC5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/etC3"
app:layout_constraintTop_toTopOf="@+id/etC1" />

<EditText
android:id="@+id/etC5"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="3dp"
android:background="@drawable/border"
android:ems="10"
android:gravity="center"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/etC1"
app:layout_constraintEnd_toStartOf="@+id/etC6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/etC4"
app:layout_constraintTop_toTopOf="@+id/etC1" />

<EditText
android:id="@+id/etC6"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@drawable/border"
android:ems="10"
android:gravity="center"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/etC1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/etC5"
app:layout_constraintTop_toTopOf="@+id/etC1"
app:layout_constraintVertical_bias="0.0" />

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Don't get the OTP?"
app:layout_constraintEnd_toStartOf="@+id/tvResendBtn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etC6" />

<TextView
android:id="@+id/tvResendBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="RESEND OTP"
android:textColor="#FF3838"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView7"
app:layout_constraintTop_toTopOf="@+id/textView7" />

<Button
android:id="@+id/btnVerify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginBottom="50dp"
android:text="VERIFY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />

<ProgressBar
android:id="@+id/progressBarVerify"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/btnVerify"
app:layout_constraintEnd_toEndOf="@+id/btnVerify"
app:layout_constraintStart_toStartOf="@+id/btnVerify"
app:layout_constraintTop_toTopOf="@+id/btnVerify" />
</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>