How to implement Biometric authentication in your Android App? – complete source code

This video shows the steps to implement the Biometric authentication in your Android App.

It uses the androidx.biometric implementation in the App’s gradle file for the same. For Biometric authentication, it uses AuthenticationCallback from BiomentricPrompt class from the implemented Biometric libraries in the App’s code.

To interpret the results, it simply uses a text view to display the results of the biometric scan whether it succeeded, failed or error.

As the biometric scanner is not available on the emulator, it uses the real device to test the App. The testing of the App on a real phone (device) is shown towards the end of the video.

I hope this video is useful to you. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Source Code:

package com.programmerworld.biometricauthenticationapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.concurrent.Executor;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private BiometricPrompt biometricPrompt;
private BiometricPrompt.PromptInfo promptInfo;
private Executor executor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);
executor = ContextCompat.getMainExecutor(this);

biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
textView.setText("Error");
}

@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);

textView.setText("Success");
}

@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();

textView.setText("Failure");
}
});

promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Programmer World Authentication")
.setNegativeButtonText("Cancel/ Use Password")
.setConfirmationRequired(false)
.build();
}

public void buttonAuthenticate(View view){
BiometricManager biometricManager = BiometricManager.from(this);
if (biometricManager.canAuthenticate() != BiometricManager.BIOMETRIC_SUCCESS){

textView.setText("Biometric Not Supported");
return;
}
biometricPrompt.authenticate(promptInfo);
}
}
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "com.programmerworld.biometricauthenticationapp"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation 'androidx.biometric:biometric:1.0.1'

}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="137dp"
android:layout_marginTop="106dp"
android:onClick="buttonAuthenticate"
android:text="@string/authenticate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply

%d bloggers like this: