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.
Practice
(1/5)
1. What is the main purpose of using BiometricPrompt in an Android app?
easy
A. To access the device camera for scanning QR codes
B. To create a custom login screen with username and password
C. To show the system biometric authentication dialog to the user
D. To manage network connections securely
Solution
Step 1: Understand the role of BiometricPrompt
BiometricPrompt is a system API that shows a standard biometric authentication dialog to the user.
Step 2: Identify its main use
It is used specifically for biometric authentication like fingerprint or face recognition, not for login screens or camera access.
Final Answer:
To show the system biometric authentication dialog to the user -> Option C
Quick Check:
BiometricPrompt = system biometric dialog [OK]
Hint: BiometricPrompt always shows system biometric dialog [OK]
Common Mistakes:
Confusing biometric prompt with custom login UI
Thinking it manages network or camera
Assuming it handles password input
2. Which of the following is the correct way to create a BiometricPrompt instance in Kotlin?
easy
A. val biometricPrompt = BiometricPrompt.create(this, executor, callback)
B. val biometricPrompt = BiometricPrompt(this, executor, callback)
C. val biometricPrompt = BiometricPrompt.newInstance(this, executor, callback)
D. val biometricPrompt = BiometricPrompt.Builder(this).build()
Solution
Step 1: Recall BiometricPrompt constructor usage
In Kotlin, BiometricPrompt is created by calling its constructor with context, executor, and callback.
Step 2: Check each option
Only val biometricPrompt = BiometricPrompt(this, executor, callback) uses the correct constructor syntax. Options A, C, and D use non-existent methods.
Final Answer:
val biometricPrompt = BiometricPrompt(this, executor, callback) -> Option B
Quick Check:
Constructor call = val biometricPrompt = BiometricPrompt(this, executor, callback) [OK]
Hint: Use constructor directly, no create() or newInstance() methods [OK]
Common Mistakes:
Using non-existent factory methods
Trying to use Builder pattern which BiometricPrompt doesn't have
Confusing with other Android APIs
3. Given this Kotlin code snippet, what will be printed if the user successfully authenticates biometrically?
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
println("Success")
}
override fun onAuthenticationFailed() {
println("Failed")
}
}
// Assume biometricPrompt is initialized and authenticate() is called with this callback
medium
A. Failed
B. Error
C. Nothing
D. Success
Solution
Step 1: Understand callback methods
The onAuthenticationSucceeded method runs when biometric authentication is successful.
Step 2: Identify printed output on success
Inside onAuthenticationSucceeded, it prints "Success". So on success, "Success" is printed.
Final Answer:
Success -> Option D
Quick Check:
Success callback prints "Success" [OK]
Hint: Success callback prints "Success" exactly [OK]
Common Mistakes:
Confusing onAuthenticationFailed with success
Assuming error prints on success
Thinking no output occurs
4. What is wrong with this Kotlin code snippet for biometric authentication?
val biometricPrompt = BiometricPrompt(this, executor, callback)
biometricPrompt.authenticate(promptInfo)
// callback object missing override for onAuthenticationError
medium
A. It will work but won't handle errors properly
B. It will crash because onAuthenticationError is not overridden
C. It will not compile due to missing method
D. It will authenticate without any callback
Solution
Step 1: Check callback completeness
Overriding onAuthenticationError is recommended but not mandatory; missing it won't crash or prevent compilation.
Step 2: Understand effect of missing error override
Without onAuthenticationError, errors won't be handled gracefully, but authentication still runs.
Final Answer:
It will work but won't handle errors properly -> Option A
Quick Check:
Missing error override = no error handling [OK]
Hint: Missing error override causes no error handling, not crash [OK]
Common Mistakes:
Assuming missing override causes crash
Thinking code won't compile
Believing authentication runs without callback
5. You want to add biometric authentication to your app but only if the device supports it and the user has enrolled biometrics. Which Kotlin code snippet correctly checks these conditions before showing the prompt?
val biometricManager = BiometricManager.from(this)
val canAuthenticate = biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.DEVICE_CREDENTIAL)
if (/* condition here */) {
biometricPrompt.authenticate(promptInfo)
}
hard
A. canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS
B. canAuthenticate != BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE
C. canAuthenticate == BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED
D. canAuthenticate == BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE
Solution
Step 1: Understand canAuthenticate return values
canAuthenticate returns BIOMETRIC_SUCCESS if biometrics are available and enrolled.
Step 2: Choose correct condition to proceed
Only when canAuthenticate == BIOMETRIC_SUCCESS should the app show the biometric prompt.
Final Answer:
canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS -> Option A
Quick Check:
Check for BIOMETRIC_SUCCESS before authenticating [OK]
Hint: Authenticate only if canAuthenticate equals BIOMETRIC_SUCCESS [OK]
Common Mistakes:
Checking for error codes instead of success
Ignoring enrollment status
Using wrong constants for condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.