How To Create One Time Password Input | Android Studio & Java
Hello guys, this tutorial I will make an OTP Online Time Password input. OTP is a verification code or a one-time password consisting of 6 unique (often numeric) secret characters that are generally sent via SMS or e-mail. Each code sent is generally only valid for 5 minutes.
Okay, first open your Android Studio, then open build.gradle (module.app) then add the following dependencies
<?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" android:orientation="vertical" android:gravity="center_vertical" android:background="#0c0f18" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textAlignment="center" android:text="MASUKKAN KODE VERIFIKASI" android:textColor="#ffffff" android:textSize="30dp" android:textStyle="bold" android:layout_marginBottom="30dp"/> <in.aabhasjindal.otptextview.OtpTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/otp_view" android:textColor="#ffffff" app:height="90dp" android:layout_gravity="center_horizontal" app:width="50dp" app:bar_enabled="true" app:bar_success_color="#43a047" app:bar_active_color="#ffee58" app:bar_height="2dp" app:length="5" app:otp_text_size="50dp" /> </LinearLayout>
When it's finished the design will look like this
Next, we open MainActivity.java and enter the following code
import androidx.appcompat.app.AppCompatActivity; import in.aabhasjindal.otptextview.OTPListener; import in.aabhasjindal.otptextview.OtpTextView; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private OtpTextView otpTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); otpTextView = findViewById(R.id.otp_view); otpTextView.setOtpListener(new OTPListener() { @Override public void onInteractionListener() { } @Override public void onOTPComplete(String otp) { if (otp.equals("12345")){ otpTextView.showSuccess(); Toast.makeText(MainActivity.this,"Kode Verifikasi Benar "+ otp, Toast.LENGTH_LONG).show(); } else{ otpTextView.showError(); Toast.makeText(MainActivity.this,"Kode Verifikasi Salah", Toast.LENGTH_LONG).show(); } } }); } }
In the code above I make a condition if the OTP TextView type the number "12345" then the OTP code is declared correct, and if otherwise it is declared false. After all is done and there are no errors, the program is ready to run.
Source Code : Github