Integrate Credential Manager with autofill

Starting with Android 15 Beta 2, paired with androidx.credentials:1.5.0-alpha01, developers can link specific views like username or password fields with Credential Manager requests. When the user focuses on one of these views, the corresponding request is sent to Credential Manager. The resulting credentials are aggregated across providers and displayed in autofill UIs, such as keyboard inline suggestions, or drop-down suggestions. This feature can be used as a fallback when users accidentally dismiss the Credential Manager account selector and then tap on the relevant fields.

The Jetpack androidx.credentials library is the preferred endpoint for developers to use for this feature.

Illustration showing credentials in autofill results
Figure 1: Autofill results with credentials using password, passkey, and Sign in with Google.

Implementation

To use Credential Manager to show credentials in autofill results, use the standard implementation to build a GetCredentialRequest and then set it to the relevant views. The response handling is the same, whether the response comes from the getCredential API call or the PendingGetCredentialRequest, as shown in the following example.

First, construct a GetCredentialRequest:

Kotlin

// Retrieves the user's saved password for your app.
val getPasswordOption = GetPasswordOption()

// Get a passkey from the user's public key credential provider.
val getPublicKeyCredentialOption = GetPublicKeyCredentialOption(
    requestJson = requestJson
)

val getCredRequest = GetCredentialRequest(
    listOf(getPasswordOption, getPublicKeyCredentialOption)
)

Java

// Retrieves the user's saved password for your app.
GetPasswordOption getPasswordOption = new GetPasswordOption();

// Get a passkey from the user's public key credential provider.
GetPublicKeyCredentialOption getPublicKeyCredentialOption =
    new GetPublicKeyCredentialOption(requestJson);

GetCredentialRequest getCredRequest = new GetCredentialRequest(
    Arrays.asList(getPasswordOption, getPublicKeyCredentialOption)
);

Next, call the getCredential API. This displays the Credential Manager selector.

Kotlin

coroutineScope.launch {
    try {
        val result = credentialManager.getCredential(
            context = activityContext, // Use an activity-based context.
            request = getCredRequest
        )
        handleSignIn(result);
    } catch (GetCredentialException e) {
        handleFailure(e);
    }
}

Java

coroutineScope.launch(new CoroutineScopeRunnable() {
    @Override
    public void run(@NonNull CoroutineScope scope) {
        try {
            GetCredentialResponse result = credentialManager.getCredential(
                activityContext, // Use an activity-based context.
                getCredRequest
            );
            handleSignIn(result);
        } catch (GetCredentialException e) {
            handleFailure(e);
        }
    }
});

Finally, enable the autofill experience. Set the getCredRequest to relevant views (such as username, password) to enable credential results in autofill when the user interacts with these views.

Kotlin

import androidx.credentials.PendingGetCredentialRequest

usernameEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(
    getCredRequest) { response -> handleSignIn(response)
}

passwordEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(
    getCredRequest) { response -> handleSignIn(response)
}

Java

import androidx.credentials.CredentialManagerViewHandler;
import androidx.credentials.PendingGetCredentialRequest;

CredentialManagerViewHandler.setPendingGetCredentialRequest(
               usernameEditText, new PendingGetCredentialRequest(
                       getCredRequest, result -> {
                           handleSignIn(result);
                           return null;
                       }
               )

CredentialManagerViewHandler.setPendingGetCredentialRequest(
               passwordEditText, new PendingGetCredentialRequest(
                       getCredRequest, result -> {
                           handleSignIn(result);
                           return null;
                       }
               )