0
0
Android Kotlinmobile~5 mins

Biometric authentication in Android Kotlin

Choose your learning style9 modes available
Introduction

Biometric authentication lets users unlock apps using their fingerprint or face. It makes apps safer and easier to use without typing passwords.

Unlocking a banking app quickly and securely.
Allowing access to private notes or files with fingerprint.
Confirming a purchase or payment with face recognition.
Logging into an app without remembering a password.
Protecting sensitive settings or information inside an app.
Syntax
Android Kotlin
val biometricPrompt = BiometricPrompt(this, ContextCompat.getMainExecutor(this),
    object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            // User authenticated successfully
        }

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            // Handle error
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            // Authentication failed
        }
    })

val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("Login with Biometrics")
    .setSubtitle("Use your fingerprint or face to login")
    .setNegativeButtonText("Cancel")
    .build()

biometricPrompt.authenticate(promptInfo)

Use BiometricPrompt to show the biometric dialog.

Always provide a negative button for fallback.

Examples
This example shows a basic biometric prompt with title, subtitle, and cancel button.
Android Kotlin
val biometricPrompt = BiometricPrompt(this, ContextCompat.getMainExecutor(this),
    object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            // Success action
        }

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            // Error handling
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            // Failed attempt
        }
    })

val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("Biometric login")
    .setSubtitle("Log in using your biometric credential")
    .setNegativeButtonText("Use password")
    .build()

biometricPrompt.authenticate(promptInfo)
Customize the prompt with a description instead of subtitle.
Android Kotlin
val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("Secure Access")
    .setDescription("Authenticate to continue")
    .setNegativeButtonText("Cancel")
    .build()
Sample App

This app shows a biometric login prompt when started. It prints messages to the console based on success, failure, or error.

Android Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val biometricPrompt = BiometricPrompt(this, ContextCompat.getMainExecutor(this),
            object : BiometricPrompt.AuthenticationCallback() {
                override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                    super.onAuthenticationSucceeded(result)
                    println("Authentication succeeded!")
                }

                override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                    super.onAuthenticationError(errorCode, errString)
                    println("Authentication error: $errString")
                }

                override fun onAuthenticationFailed() {
                    super.onAuthenticationFailed()
                    println("Authentication failed")
                }
            })

        val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle("Login with Biometrics")
            .setSubtitle("Use your fingerprint or face to login")
            .setNegativeButtonText("Cancel")
            .build()

        biometricPrompt.authenticate(promptInfo)
    }
}
OutputSuccess
Important Notes

Test biometric features on a real device with fingerprint or face setup.

Handle cases when biometric hardware is not available or not enrolled.

Always provide a fallback option like PIN or password.

Summary

Biometric authentication improves app security and user convenience.

Use BiometricPrompt to show the system biometric dialog.

Always handle success, failure, and errors gracefully.