Custom BiometricPrompt.Builder

Viewed 5139

Can we modify/custom ourBiometricPrompt?

For example right now i use smth like this:

BiometricPrompt.Builder(AppResources.appContext)
            .setTitle("title")
            .setSubtitle("subTitle")
            .setDescription("description")
            .setNegativeButton("Cancel", AppResources.appContext?.mainExecutor,
                    DialogInterface.OnClickListener { dialogInterface, i -> biometricCallback.onAuthenticationCancelled() })
            .build()
            .authenticate(CancellationSignal(), AppResources.appContext?.mainExecutor,
                    BiometricCallbackV28(biometricCallback))

Do I have the ability to change the style of the text, title, negativeButton color?

3 Answers

Update2: beta01

The change with the alpha version was that the fingerprint dialog uses android.app.AlertDialog and the new beta01 uses androidx.appcompat.app.AlertDialog which has a private style.

Then to override that style we have to override androidx.appcompat.R.attr.alertDialogTheme reference in our styles.xml, we can do it replacing <item name="android:alertDialogTheme">@style/CustomAlertTheme</item> by <item name="alertDialogTheme">@style/CustomAlertTheme</item> in my case thay I only wanted to change the button text color, I just added <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item> and the same for posivtive, because replacing alertDialogTheme changes other things that I did not want to.

My styles.xml now:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:buttonStyle">@style/WibleButtonStyle</item>
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
 </style>

<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/dark_blue</item>
</style>

Update: beta01

This trick is not working anymore. I'll try if I can find another way to do it.


I found two ways to do it, none of these are specific for the BiometricPrompt, one is to override the theme button style: <item name="android:buttonStyle">@style/CustomButtonStyle</item>

another option is to override your alert dialog theme:

AppTheme style

<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>

    <style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
        <item name="android:buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
        <item name="android:buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    </style>

    <style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textSize">10sp</item>
        <item name="android:textColor">@color/primary_text</item>
    </style>

    <style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">@color/primary_text</item>
    </style>

And then you can apply a specific theme to a specific activity. Maybe they could open the API to allow styles, but I had to change the color because it was impossible to see the buttons (everything was white) and this is what I found to solve my problem. I hope it helps you.

You can't set properties on the prompt that aren't exposed through its Builder - the UI is provided by the system, and is designed to be uniform throughout all apps.

This is sort of the main point of this API, this way the user becomes familiar with the prompt and knows that whatever they're interacting with is safe to use.

Can we modify/custom our BiometricPrompt?

The BiometricPrompt uses the "buttonStyle" style that is set for your theme, which you can adjust in the styles.xml Keep in mind that this is the default button style, so if you do not want other buttons to change you would have to assign buttons etc. their own style. (and dialogs etc.)

For example, if you only want the text color to be different for the BiometricPrompt, you could do something like this:

<style name="myTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="buttonStyle">@style/ButtonPrimaryStyle</item>
</style>

<style name="ButtonPrimaryStyle" parent="ButtonParentStyle">
    <item name="android:textColor">454545</item>
</style>

<style name="ButtonPrimaryStyleMain" parent="ButtonPrimaryStyle">
    <item name="android:textColor">e2e2e2</item>
</style>

This would make the BiometricPrompt have a dark grey text for the cancel button, while every button with the "ButtonPrimaryStyleMain" style will have a lightgrey, almost white, text color.

You would also have to assign this theme to your application in the manifest.

<application
    android:theme="@style/myTheme"
Related