How to Add Fingerprint Authentication in Your Android App?
Nowadays, we can find fingerprint authentication on the majority of our Android phones. And we can use fingerprint authentication in our app to ensure that it is as secure as possible. In this post, we'll look at how fingerprint authentication is implemented.
We'll make a basic application that shows a fingerprint image and a login button. We will apply our fingerprint after hitting the login button. We will be able to log in successfully if the identical fingerprint is entered to the Security option. A sample movie is provided below to give you an idea of what we'll be doing in this article. It's worth noting that we'll be using the Java programming language to complete this project.
Step 1: Create a New Project
Step 2: Add dependency to gradle
// Java language implementation
implementation "androidx.biometric:biometric:1.1.0"
// Kotlin
implementation "androidx.biometric:biometric-ktx:1.2.0-alpha04"
Step 3: Working with the AndroidManifst.xml file
<uses-permission android:name=”android.permission.USE_BIOMETRIC”/>
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
<?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">
<Button
android:layout_width="150dp"
android:layout_height="180dp"
android:background="@drawable/fing"
android:id="@+id/btn"
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 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file
package com.example.fingerauth;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BiometricPrompt.PromptInfo promptInfo=new BiometricPrompt.PromptInfo.Builder()
.setTitle("Please Authenticate")
.setDescription("User verification is Required")
.setNegativeButtonText("Cancel")
.build();
getBiometricprompt().authenticate(promptInfo);
}
});
}
private BiometricPrompt getBiometricprompt(){
Executor executor= ContextCompat.getMainExecutor(this);
BiometricPrompt.AuthenticationCallback callback=new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(MainActivity.this, "Verified", Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
};
BiometricPrompt biometricPrompt=new BiometricPrompt(this,executor,callback);
return biometricPrompt;
}
}
Comments
Post a Comment