Integrate Credential Manager with autofill

Starting with Android 15, developers can link specific views like username or password fields with Credential Manager requests, making it easier to provide a tailored user experience during the sign-in process. When the user focuses on one of these views, a corresponding request is sent to Credential Manager. The resulting credentials are aggregated across providers and displayed in autofill fallback UIs, such as inline suggestions or drop-down suggestions. The Jetpack androidx.credentials library is the preferred endpoint for developers to use and will soon be available to further enhance this feature in Android 15 and higher.

Implementation

To integrate Credential Manager results with Android autofill, you build on the GetCredentialRequest as in the standard implementation by also applying the response to fields that should show the same suggestions in fallback UIs. The response handling remains consistent, whether it comes from the getCredential API call or the PendingGetCredentialRequest configured as shown in the following example.

First, follow the steps to sign in your user by constructing a GetCredentialRequest:

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 fallback user experience. Set the getCredRequest to relevant views (e.g., username, password) to enable fallback user experiences 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.PendingGetCredentialRequest;

usernameEditText.setPendingGetCredentialRequest(new PendingGetCredentialRequest(
    getCredRequest,
    response -> handleSignIn(response)
));

passwordEditText.setPendingGetCredentialRequest(new PendingGetCredentialRequest(
    getCredRequest,
    response -> handleSignIn(response)
));