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

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