Deploy your apps to a supercloud in a few clicks

This Engineering Education program is supported by Section. Instantly deploy your GitHub apps, Docker containers or K8s namespaces to a supercloud.

Try It For Free

Implementing Fingerprint Authentication in Android Applications

May 31, 2021

Biometrics are calculations and body measurements that are related to human characteristics. Biometrics are categorized into: Physical and behavioral. Physical biometrics is our humanly features such as our fingerprints, iris, and many others. Behavioral is our actions, for example, smiling, hand signs, and others.

In turn, biometrics has brought up vast, easy, and more secure ways to identify a specific user using their physical traits.


Let us admit it, typing over eight characters in the name of a password is quite a task, that is if a user even remembers the password. These days most developers and companies have made it easier for us by using biometrics as a form of authentication.

Biometrics, time and time again, have proved to be more efficient and less prone to cyber-attacks. Most fields such as Banking, Information Technology, Education, and many others have implemented biometrics into their systems for various purposes.

This article will cover one of the biometric methods of authentication, which is using our fingerprint.

Advantages of Biometrics

  • Biometrics is specific because it is associated with a single person (unlike a password that anyone can authorize).
  • It is very convenient. There is no need to carry or remember anything.
  • It is very secure. Biometrics is highly fraud-resistant.


  • They are error-prone, sometimes even causing the system to shut down. Once biometric data has been compromised, there is no way to undo the damage. For a compromised password, you simply change it; for a fingerprint, ear image, or iris scan, you’re stuck with the compromised biometric which eventually leads to the shut down of a biometric system.
  • Biometric devices and in-built biometric features are costly compared to other traditional devices.



By the end of this tutorial, the reader should be able to:

  • Understand what biometrics are and the various applications of biometrics.
  • Understand how to implement fingerprint authentication into an Android application.

Now let’s dive in and build our application:

Step One: Create a new Android Studio project

Open Android Studio and select start a new Android Studio Project -> Empty Activity.

We will name the project FingerprintAuthentication. Select Finish and wait for the project to build.

Create a new Android Studio project

Step Two: Enable the biometrics permission in the manifest file

In our AndroidManifest.xml file, we will add the biometrics permission statement, which will allow our application to access the inbuilt biometrics features in our devices.

Add the following line of code in your AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_BIOMETRIC" />

Step Three: Adding the biometrics library to our project

Add the following dependencies to your app-level build.gradle file.

def biometricLibraryVersion = "1.0.1"
implementation "androidx.biometric:biometric:$biometricLibraryVersion"

Step Four: Create a new Empty Activity

Since our application will require us to use biometrics to access a top-secret activity, we will create an activity that will contain a secret message.

Right-click the Java directory and select New -> Activity -> Empty Activity. Let us name our new activity Secret. Click Finish.

Our Secret Activity’s UI will only contain a Textview which will display an optional message.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
        android:text="TOP SECRET MESSAGE :)"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout />

Step Five: Update MainActivity.kt

In this step, we will write code that will enable our Activity to authenticate a user using biometrics to access our top-secret Activity.

Our code will contain the following functions:

  • An onAuthenticationError function, that provides the outcome of a failed authentication instance.
  • An onAuthenticationSucceeded function, which provides the outcome of a successful authentication instance.
  • An onCreate function, which will contain an onClickListener that will bring up the Biometric prompt. We will also include some details to our biometric prompt in this function.
  • A notifyUser function that will display a Toast message whenever called in another function.
  • A getCancellationSignal function, that will handle cancellations made by the user.
  • A checkBiometricSupport function will check whether a particular Android device has inbuilt biometrics support, whether they are enabled in the phone’s settings, or the user has accepted the app’s permission to access the biometrics.
package com.example.fingerprintauthentication
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
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() {
    private var cancellationSignal: CancellationSignal? = null
    private val  authenticationCallback: BiometricPrompt.AuthenticationCallback
    get() =
        object: BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
                super.onAuthenticationError(errorCode, errString)
                notifyUser("Authentication error: $errString")
            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
                notifyUser("Authentication Success!")
    override fun onCreate(savedInstanceState: Bundle?) {
        val button = findViewById<Button>(
            val biometricPrompt : BiometricPrompt = BiometricPrompt.Builder(this)
                .setSubtitle("Authenticaion is required")
                .setDescription("Fingerprint Authentication")
                .setNegativeButton("Cancel", this.mainExecutor, DialogInterface.OnClickListener { dialog, which ->
            biometricPrompt.authenticate(getCancellationSignal(), mainExecutor, authenticationCallback)
    private fun notifyUser(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    private fun getCancellationSignal(): CancellationSignal {
        cancellationSignal = CancellationSignal()
        cancellationSignal?.setOnCancelListener {
            notifyUser("Authentication was cancelled by the user")
        return cancellationSignal as CancellationSignal
    private fun checkBiometricSupport(): Boolean {
        val keyguardManager : KeyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
        if(!keyguardManager.isKeyguardSecure) {
            notifyUser("Fingerprint hs not been enabled in settings.")
            return false
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.USE_BIOMETRIC) !=PackageManager.PERMISSION_GRANTED) {
            notifyUser("Fingerprint hs not been enabled in settings.")
            return false
        return if (packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
        } else true

We are done!

Let’s run our app.

Fingerprint authentication

Wrapping up

We have learned how to implement biometrics in an Android application. We have also known the advantages and disadvantages of using biometrics.

Read more about biometrics from the official Android developer’s page and practice.

Remember, practice makes perfect.

You can access the tutorial’s code on GitHub. You can also download the sample APK on Google Drive.

For any query or clarification, do not hesitate to raise an issue in this repository.

Till next time, Happy coding!

Peer Review Contributions by: Ahmad Mardeni