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 ❖❖

7. Switch Statement in c++

 ❖ C++ switch Statement :-                Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.  Syntax of switch:-      Switch(expression) {            Case value1 : statement1;                       Break;             Case value2 : statement2;                      Break;             Default : default statements;  }               The expression is either an integer or a character in above syntax. If the expression matches constant in case, the relevant codes a...

3. Input /output in c++

  ➢   Input/ Output in C++ :-        C++ comes with libraries which provides us many ways for performing input and output. In C++.        input and output is performed in the form of sequence of bytes or more commonly known as  streams. ● Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input. ● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device(display screen ) then this process is called output. ● Header files available in C++ for Input – Output operation are: • iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc. • fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.       ...