Open In App

How to Add Fingerprint Authentication in Your Android App without Using any Library?

Last Updated : 27 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 both Java and 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/Java 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




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <Button
        android:id="@+id/start_authentication"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Authenticate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Working with the MainActivity file 

Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail. 

Kotlin




import android.app.KeyguardManager
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.hardware.biometrics.BiometricPrompt
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CancellationSignal
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
 
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?) {
                    super.onAuthenticationSucceeded(result)
                    notifyUser("Authentication Succeeded")
                     
                    // or start a new Activity
                     
                }
            }
 
    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        checkBiometricSupport()
        // create a biometric dialog on Click of button
        findViewById<Button>(R.id.start_authentication).setOnClickListener {
            // 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")
                    .setSubtitle("Subtitle")
                    .setDescription("Uses FP")
                    .setNegativeButton("Cancel", this.mainExecutor, DialogInterface.OnClickListener { dialog, which ->
                        notifyUser("Authentication Cancelled")
                    }).build()
 
            // 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
    @RequiresApi(Build.VERSION_CODES.M)
    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)) {
            true
        } 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()
    }
}


Java




import android.app.KeyguardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
 
public class MainActivity extends AppCompatActivity {
 
    // create a CancellationSignal
      // variable and assign a
    // value null to it
    private CancellationSignal cancellationSignal = null;
 
    // create an authenticationCallback
    private BiometricPrompt.AuthenticationCallback authenticationCallback;
 
    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void
    onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        authenticationCallback = new 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
                  public void onAuthenticationError(
                      int errorCode, CharSequence errString)
                  {
                      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
                  public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result)
                  {
                      super.onAuthenticationSucceeded(result);
                      notifyUser("Authentication Succeeded");
                      // or start a new Activity
                  }
              };
 
        checkBiometricSupport();
        // create a biometric dialog on Click of button
        (Button) findViewById(R.id.start_authentication).setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.P)
                @Override
                public void onClick(View view)
                {
                    // 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
                    BiometricPrompt biometricPrompt = new BiometricPrompt
                              .Builder(getApplicationContext())
                              .setTitle("Title of Prompt")
                              .setSubtitle("Subtitle")
                              .setDescription("Uses FP")
                              .setNegativeButton("Cancel", getMainExecutor(), new DialogInterface.OnClickListener() {
                                          @Override
                                          public void
                                          onClick(DialogInterface dialogInterface, int i)
                                          {
                                              notifyUser("Authentication Cancelled");
                                          }
                                      }).build();
 
                    // start the authenticationCallback in
                    // mainExecutor
                    biometricPrompt.authenticate(
                        getCancellationSignal(),
                        getMainExecutor(),
                        authenticationCallback);
                }
            });
    }
 
    // it will be called when
      // authentication is cancelled by
    // the user
    private CancellationSignal getCancellationSignal()
    {
        cancellationSignal = new CancellationSignal();
        cancellationSignal.setOnCancelListener(
            new CancellationSignal.OnCancelListener() {
                @Override public void onCancel()
                {
                    notifyUser("Authentication was Cancelled by the user");
                }
            });
        return cancellationSignal;
    }
 
    // it checks whether the
      // app the app has fingerprint
    // permission
    @RequiresApi(Build.VERSION_CODES.M)
    private Boolean checkBiometricSupport()
    {
        KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
        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;
        }
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
            return true;
        }
        else
            return true;
    }
 
    // this is a toast method which is responsible for
    // showing toast it takes a string as parameter
    private void notifyUser(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}


Output:

GitHub repo here.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads