install android studio

FingerPrint Authentication in Android Studio

FingerPrint authentication:

Recently with the release of Android 6.0 (M) last year, there has been a significant amount of changes to the APIs, one of them is Fingerprint Authentication. With the release of new APIs, authenticating users with help of fingerprint sensors on various devices is possible.
Following tutorial example shows how to implement Fingerprint Authentication in your application.

To authenticate users using the fingerprint sensor, you need to get an instance of the newly implemented FingerprintManager class and call the authenticate() method. However your app must be running on a compatible device which includes a fingerprint sensor. Moreover you must implement the user interface for the fingerprint authentication flow on your app, and use the standard Android fingerprint icon in your UI. Note that If you are developing multiple apps that use fingerprint authentication, each app must authenticate the user’s fingerprint separately.

1. Create New Project :

Make a new Project and select API 23: Android 6.0 (Marshmallow) min. SDK for FingerPrint Authentication.

2. Permissions:


uses-permission android:name="android.permission.USE_FINGERPRINT" />

3. Add image :

Add this type of image for authentication named as “fingerprint_image”.

4. activity_main.xml:


?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/activity_fingerprint"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/colorPrimary"
   tools:context="info.androidhive.fingerprint.FingerprintActivity">
 
 
   LinearLayout
       android:layout_width="match_parent"
       android:id="@+id/headerLayout"
       android:orientation="vertical"
       android:gravity="center"
       android:layout_marginTop="100dp"
       android:layout_height="wrap_content">
 
 
       ImageView
           android:layout_width="70dp"
           android:layout_height="70dp"
           android:src="@drawable/fingerprint_image"
           android:id="@+id/icon"
           android:paddingTop="2dp"
           android:layout_marginBottom="30dp"/>
 
 
       TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="@color/textPrimary"
           android:textSize="24sp"
           android:text="@string/title_fingerprint"
           android:layout_marginLeft="5dp"
           android:layout_marginRight="5dp"
           android:layout_marginTop="20dp"
           android:layout_marginBottom="10dp"/>
 
       TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textColor="@color/textPrimary"
           android:textSize="16sp"
           android:textAlignment="center"
           android:gravity="center"
           android:id="@+id/desc"
           android:text="@string/desc_fingerprint"
           android:layout_margin="16dp"
           android:paddingEnd="30dp"
           android:paddingStart="30dp"/>
 
 
       TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textColor="@color/errorText"
           android:textSize="14sp"
           android:textAlignment="center"
           android:id="@+id/errorText"
           android:paddingEnd="30dp"
           android:paddingStart="30dp"
           android:layout_marginTop="30dp"
           android:gravity="center"/>
 
   /LinearLayout>
   TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="@color/textPrimaryDark"
       android:textSize="14sp"
       android:text="@string/note"
       android:layout_marginLeft="16dp"
       android:textAlignment="center"
       android:layout_marginRight="16dp"
       android:layout_marginBottom="26dp"
       android:layout_alignParentBottom="true"/>
 
/RelativeLayout>

5. MainActivity.java:


?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/activity_fingerprint"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/colorPrimary"
   tools:context="info.androidhive.fingerprint.FingerprintActivity">
 
 
   LinearLayout
       android:layout_width="match_parent"
       android:id="@+id/headerLayout"
       android:orientation="vertical"
       android:gravity="center"
       android:layout_marginTop="100dp"
       android:layout_height="wrap_content">
 
 
       ImageView
           android:layout_width="70dp"
           android:layout_height="70dp"
           android:src="@drawable/ic_action_fingerprint"
           android:id="@+id/icon"
           android:paddingTop="2dp"
           android:layout_marginBottom="30dp"/>
 
 
       TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="@color/textPrimary"
           android:textSize="24sp"
           android:text="@string/title_fingerprint"
           android:layout_marginLeft="5dp"
           android:layout_marginRight="5dp"
           android:layout_marginTop="20dp"
           android:layout_marginBottom="10dp"/>
 
       TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textColor="@color/textPrimary"
           android:textSize="16sp"
           android:textAlignment="center"
           android:gravity="center"
           android:id="@+id/desc"
           android:text="@string/desc_fingerprint"
           android:layout_margin="16dp"
           android:paddingEnd="30dp"
           android:paddingStart="30dp"/>
 
 
       TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textColor="@color/errorText"
           android:textSize="14sp"
           android:textAlignment="center"
           android:id="@+id/errorText"
           android:paddingEnd="30dp"
           android:paddingStart="30dp"
           android:layout_marginTop="30dp"
           android:gravity="center"/>
 
   /LinearLayout>
   TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="@color/textPrimaryDark"
       android:textSize="14sp"
       android:text="@string/note"
       android:layout_marginLeft="16dp"
       android:textAlignment="center"
       android:layout_marginRight="16dp"
       android:layout_marginBottom="26dp"
       android:layout_alignParentBottom="true"/>
 
/RelativeLayout>

After successful authentication of fingerprint scanner , HomeActivity will appear.


Problem will be discussed in Comment section.

About the author

Generic Bytes

Hi! we are from Generic Bytes an aspiring bloggers with an obsession of things tech.This blog is dedicated to people who want to learn the technology.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *