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

Remove specific item from Recyclerview in android studio

For Full video click on below link :- https://youtu.be/sUz4fqeanjI?si=A8AqBWCgu-5NnBOJ 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: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" android:orientation="vertical" android:background="@color/white"> <EditText android:layout_width="350dp" android:layout_height="50dp" android:inputType="text" android:id="@+id/coursenam...

programming skills imp programs

Full course of c++ programming language Click here >  C++ programming language ===================================== 1. Write a program to display an average of 3 inputted numbers. a=int(input("Enter a : ")) b=int(input("Enter a : ")) c=int(input("Enter a : ")) avg=(a+b+c)/3 print(avg) ====================================== 2. Write a program to calculate simple interest for a given principle amount, rate of interest and no of years. p=float(input("Enter a P : ")) r=float(input("Enter a R : ")) n=float(input("Enter a N : ")) si=(p*r*n)/100 print("simple interest is ",si) ====================================== 3. Write a program to display area of triangle. base=float(input("Enter a base : ")) height=float(input("Enter a height : ")) area=base*height/2 print("Area of triangle is ",area) ====================================== 4. Write a program to calculate area of circle. r=float(input...