Exploring Android 12: BiometricPrompt Localizable Strings

Another simple new feature introduced by Android 12 is BiometricPrompt Localizable Strings.

Essentially, developers will be able to access three new methods to get localized strings related to biometric prompt:

  1. getLabelButton(): Gets button label that invokes the biometric prompt.
  2. getPromptMessage(): Gets message shown while the user is authenticating with biometric prompt.
  3. getSettingName(): Gets title of an app setting that enables authentication with biometric prompt.

You can use these strings for your UI elements instead of creating it yourself in strings.xml file.

In this article, we will create a simple application that demonstrates this new methods. The final result can be shown in the image below:

BiometricPrompt Localizable Strings in action. Left image shows the strings in English (en) and right image shows the strings in Indonesian (id).

Where’s the Source Code?

Step by Step Explanation

Follow the steps provided below to create this application.

1. Open Android Studio. Create new project with “Empty Activity” option.

2. First, we need to add Biometric library to our application. Open app/build.gradle file and add this dependency:

3. Next, we will update our layout, activity_main.xml file. The image below shows how the layout will look like. It will contain TextViews for displaying the localized button label, prompt message, and setting name.

This is what activity_main.xml file should look like.

To make this layout, copy-paste the code snippet below:

4. Lastly, we need to update our MainActivity.kt file. Go ahead and copy-paste the code snippet below. There are some important things to notice here. First, the usage of BiometricManager.getStrings() for retrieving the localized strings. You need to pass authenticators to this method based on the types of authentication that you want to use in your app. Different authenticators will result in different localized strings.

Another thing to notice here is obviously the usage of getButtonLabel(), getPromptMessage(), and getSettingName() methods to retrieve the localized strings.

5. That’s it! Now run your app and see the localized strings for yourself. Change your device’s language in the Settings to see the different strings like this:

BiometricPrompt Localizable Strings in action. Left image shows the strings in English (en) and right image shows the strings in Indonesian (id).

As always, thanks for reading!

Want to learn more about Android 12?

Check out all of our articles from “Exploring Android 12” series here:

Exploring Android 12

9 stories

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store