Skip to main content

Fingerprint Authentication in Android Studio Project

Implement the implementation : -


implementation 'androidx.biometric:biometric:1.0.1'


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"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@color/white">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Boxcode!!"
android:textSize="30dp"
android:textColor="@color/black"/>

<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="@color/black"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btn"
android:layout_width="155dp"
android:layout_height="55dp"
android:text="Login"
android:textColor="@color/white"
android:backgroundTint="@color/black"
android:layout_marginTop="40dp"/>
</LinearLayout>



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.fingerprint;


import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

import java.util.concurrent.Executor;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView msg=findViewById(R.id.msg);
final Button btn=findViewById(R.id.btn);

androidx.biometric.BiometricManager biometricManager=androidx.biometric.BiometricManager.from(this);

switch(biometricManager.canAuthenticate()){

case BiometricManager.BIOMETRIC_SUCCESS:
msg.setText("YOU CAN USE THE FINGERPRINT SENSOR TO LOGIN");
break;

case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
msg.setText("THIS DEVICE DOES NOT HAVE A FINGERPRINT SENSOR");
btn.setVisibility(View.GONE);
break;

case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
msg.setText("THE BIOMETRIC SENSOR IS CURRENTLY UNAVAILABLE");
btn.setVisibility(View.GONE);
break;

case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
msg.setText("YOUR DEVICE DOESN'T HAVE FINGERPRINT SAVED,PLEASE CHECK YOUR SECURITY SETTING");
btn.setVisibility(View.GONE);
break;
}

Executor executor=ContextCompat.getMainExecutor(this);

final BiometricPrompt biometricPrompt=new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}

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

Toast.makeText(MainActivity.this, "Login success", Toast.LENGTH_SHORT).show();
btn.setText("Login Successful");
}

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

final BiometricPrompt.PromptInfo promptInfo=new BiometricPrompt.PromptInfo.Builder().setTitle("BOXCODE").setDescription("Use Your Fingerprint to Login").setNegativeButtonText("Cancel").build();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
biometricPrompt.authenticate(promptInfo);
}
});
}
}

Comments

Popular posts from this blog

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

Pratical -206 paper solution -3

Full course of c++ programming language Click here > C++ programming language Q-1 [A] Write a c program to create structure of employee with members Empid, EmpName, Qualification and EmpSalary by taking input of 5 employees display the employee whose qualification is "MBA" and salary greater than 20000 . #include <stdio.h> #include <conio.h> #include <string.h> struct Employee {     int Empid, EmpSalary;     char EmpName[50];     char Qualification[50]; }; Void main() {     struct Employee emp[5];     int i;     for (i = 0; i < 5; i++)     {         printf("Enter employee ID: ");         scanf("%d", &emp[i].Empid);         printf("Enter employee name: ");         scanf("%s", emp[i].EmpName);         printf("Enter employee qualification: ");         scanf("%s", emp[i].Quali...

9. Jumps Statement in c++

➢ Jumps in Loops:- ➢ C++ break and continue Statement:-                     There are two statements (break; and continue ;) built in C++ programming to alter the normal flow of program.                     Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement. 1) break Statement :-                       The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears . Syntax of break :- break;              ...