Skip to content
Related Articles

Related Articles

How to Add Fingerprint Authentication in Your Android App without Using any Library?
  • Last Updated : 18 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

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 version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />

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.


import android.content.Context
import android.content.DialogInterface
import android.hardware.biometrics.BiometricPrompt
import android.os.Build
import android.os.Bundle
import android.os.CancellationSignal
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
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?) {
                    notifyUser("Authentication Succeeded")
                    // or start a new Activity
    override fun onCreate(savedInstanceState: Bundle?) {
        // create a biometric dialog on Click of button
        findViewById<Button>( {
            // 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")
                    .setDescription("Uses FP")
                    .setNegativeButton("Cancel", this.mainExecutor, DialogInterface.OnClickListener { dialog, which ->
                        notifyUser("Authentication Cancelled")
            // 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
    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)) {
        } 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()


Github repo here.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :