Free Tutorial Service Computer and Android Studio

Biometric Authentication | Android Studio | Kotlin

by Tutorial , at 11/12/2019 09:15:00 AM

How to do Biometric Authentication?

In this tutorial, we will learn how to use Biometric Authentication e.g. Fingerprint Authentication/Login. There may be some features in your app that needs user authentication. So here is the simplest example of using Biometric Authentication.

>>Check For Java

Video:

Step 1: Create a new project OR Open your existing project

Step 2: Code

build.gradle

Add the biometric library to app-level build.gradle file
dependencies {
implementation 'androidx.biometric:biometric:1.0.1'
}

AndroidManifest.xml

Add the USE_BIOMETRIC dependency to the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wongngaur.blogspot.biometricauthentication">

<!--Add permission-->
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="50dp"
tools:context=".MainActivity">

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:src="@drawable/lock_vector"/>

<TextView
android:id="@+id/authStatusTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#000"
android:textSize="20sp"
android:text="Click button to start authentication" />

<Button
android:id="@+id/authBtn"
android:layout_below="@id/authStatusTv"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Authenticate"
style="@style/Widget.AppCompat.Button.Colored"
android:drawableStart="@drawable/ic_fingerprint_white"
android:drawablePadding="10dp"/>

</RelativeLayout>

MainActivity.kt

package com.wongngaur.blogspot.biometricauthentication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.Executor

class MainActivity : AppCompatActivity() {

private lateinit var executor: Executor
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//init biometric
executor = ContextCompat.getMainExecutor(this)

biometricPrompt = BiometricPrompt(this@MainActivity, executor, object : BiometricPrompt.AuthenticationCallback(){
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
//auth error, stop tasks that requires auth
authStatusTv.text = "Authentication Error: $errString"
Toast.makeText(this@MainActivity, "Authentication Error: $errString", Toast.LENGTH_SHORT).show()
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
//auth succeed, do tasks that requires auth
authStatusTv.text = "Auth succeed...!"
Toast.makeText(this@MainActivity, "Auth succeed...!", Toast.LENGTH_SHORT).show()

}

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
//auth failed, stop tasks that requires auth
authStatusTv.text = "Auth failed...!"
Toast.makeText(this@MainActivity, "Auth failed...!", Toast.LENGTH_SHORT).show()
}
})

//set properties like title and description on auth dialog
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric Authentication")
.setSubtitle("Login using fingerprint authentication")
.setNegativeButtonText("Use App Password instead")
.build()

//handle click, start authtenitcation dialog
authBtn.setOnClickListener {
//show auth dialog
biometricPrompt.authenticate(promptInfo)
}
}
}





Biometric Authentication | Android Studio | Kotlin
Biometric Authentication | Android Studio | Kotlin - written by Tutorial , published at 11/12/2019 09:15:00 AM, categorized as android , android studio , authenticate , biometric , biometric authentication , biometric login , fingerprint , fingerprint authentication , fingerprint login , fingerprint scanner , fingerprintmanager , Kotlin , tutorial
Comment disabled
Theme designed by ngaur.com - Published by ngaur.com
Powered by Blogger