Biometric Fingerprint authentication is one method of protecting sensitive information or premium content of your app. Nowadays all payment apps use this feature in their app. It is very easy to implement. A sample Video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language
Step by Step Implementation
Step 1: Create a new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language
Step 2: Add Biometric permission in the manifest
Go to the AndroidMenifest.xml file and add the following permission there.
<uses-permission android:name=”android.permission.USE_BIOMETRIC”/>
Step 3: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. It has only a Button on click of which will create the fingerprint Scanner dialog box.
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/start_authentication" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Authenticate" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.app.KeyguardManagerimport android.content.Contextimport android.content.DialogInterfaceimport android.content.pm.PackageManagerimport android.hardware.biometrics.BiometricPromptimport android.os.Buildimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.os.CancellationSignalimport android.widget.Buttonimport android.widget.Toastimport androidx.annotation.RequiresApiimport androidx.core.app.ActivityCompat class MainActivity : AppCompatActivity() { // create a CancellationSignal variable and assign a value null to it private var cancellationSignal: CancellationSignal? = null // create an authenticationCallback private val authenticationCallback: BiometricPrompt.AuthenticationCallback get() = @RequiresApi(Build.VERSION_CODES.P) object : BiometricPrompt.AuthenticationCallback() { // here we need to implement two methods // onAuthenticationError and onAuthenticationSucceeded // If the fingerprint is not recognized by the app it will call // onAuthenticationError and show a toast override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) { super.onAuthenticationError(errorCode, errString) notifyUser("Authentication Error : $errString") } // If the fingerprint is recognized by the app then it will call // onAuthenticationSucceeded and show a toast that Authentication has Succeed // Here you can also start a new activity after that override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) { super.onAuthenticationSucceeded(result) notifyUser("Authentication Succeeded") // or start a new Activity } } @RequiresApi(Build.VERSION_CODES.P) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) checkBiometricSupport() // create a biometric dialog on Click of button findViewById<Button>(R.id.start_authentication).setOnClickListener { // This creates a dialog of biometric auth and // it requires title , subtitle , // and description // In our case there is a cancel button by // clicking it, it will cancel the process of // fingerprint authentication val biometricPrompt = BiometricPrompt.Builder(this) .setTitle("Title of Prompt") .setSubtitle("Subtitle") .setDescription("Uses FP") .setNegativeButton("Cancel", this.mainExecutor, DialogInterface.OnClickListener { dialog, which -> notifyUser("Authentication Cancelled") }).build() // start the authenticationCallback in mainExecutor biometricPrompt.authenticate(getCancellationSignal(), mainExecutor, authenticationCallback) } } // it will be called when authentication is cancelled by the user private fun getCancellationSignal(): CancellationSignal { cancellationSignal = CancellationSignal() cancellationSignal?.setOnCancelListener { notifyUser("Authentication was Cancelled by the user") } return cancellationSignal as CancellationSignal } // it checks whether the app the app has fingerprint permission @RequiresApi(Build.VERSION_CODES.M) private fun checkBiometricSupport(): Boolean { val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager if (!keyguardManager.isDeviceSecure) { notifyUser("Fingerprint authentication has not been enabled in settings") return false } if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.USE_BIOMETRIC) != PackageManager.PERMISSION_GRANTED) { notifyUser("Fingerprint Authentication Permission is not enabled") return false } return if (packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { true } else true } // this is a toast method which is responsible for showing toast // it takes a string as parameter private fun notifyUser(message: String) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() }} |
Output:
Github repo here.



