Skip to content

Most visited

Recently visited

navigation

AutofillManager

public final class AutofillManager
extends Object

java.lang.Object
   ↳ android.view.autofill.AutofillManager


The AutofillManager provides ways for apps and custom views to integrate with the Autofill Framework lifecycle.

The autofill lifecycle starts with the creation of an autofill context associated with an activity context; the autofill context is created when one of the following methods is called for the first time in an activity context, and the current user has an enabled autofill service:

Tipically, the context is automatically created when the first view of the activity is focused because View.onFocusChanged() indirectly calls notifyViewEntered(View). App developers can call requestAutofill(View) to explicitly create it (for example, a custom view developer could offer a contextual menu action in a text-field view to let users manually request autofill).

After the context is created, the Android System creates a ViewStructure that represents the view hierarchy by calling dispatchProvideAutofillStructure(android.view.ViewStructure, int) in the root views of all application windows. By default, dispatchProvideAutofillStructure() results in subsequent calls to onProvideAutofillStructure(android.view.ViewStructure, int) and onProvideAutofillVirtualStructure(android.view.ViewStructure, int) for each view in the hierarchy.

The resulting ViewStructure is then passed to the autofill service, which parses it looking for views that can be autofilled. If the service finds such views, it returns a data structure to the Android System containing the following optional info:

  • Datasets used to autofill subsets of views in the activity.
  • Id of views that the service can save their values for future autofilling.

When the service returns datasets, the Android System displays an autofill dataset picker UI affordance associated with the view, when the view is focused on and is part of a dataset. The application can be notified when the affordance is shown by registering an AutofillManager.AutofillCallback through registerCallback(AutofillCallback). When the user selects a dataset from the affordance, all views present in the dataset are autofilled, through calls to autofill(AutofillValue) or autofill(SparseArray).

When the service returns ids of savable views, the Android System keeps track of changes made to these views, so they can be used to determine if the autofill save UI is shown later.

The context is then finished when one of the following occurs:

Finally, after the autofill context is commited (i.e., not cancelled), the Android System shows a save UI affordance if the value of savable views have changed. If the user selects the option to Save, the current value of the views is then sent to the autofill service.

It is safe to call into its methods from any thread.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument AutofillManager.class.

Summary

Nested classes

class AutofillManager.AutofillCallback

Callback for autofill related events. 

Constants

String EXTRA_ASSIST_STRUCTURE

Intent extra: The assist structure which captures the filled screen.

String EXTRA_AUTHENTICATION_RESULT

Intent extra: The result of an authentication operation.

String EXTRA_CLIENT_STATE

Intent extra: The optional extras provided by the AutofillService.

Public methods

void cancel()

Called to indicate the current autofill context should be cancelled.

void commit()

Called to indicate the current autofill context should be commited.

void disableAutofillServices()

If the app calling this API has enabled autofill services they will be disabled.

boolean hasEnabledAutofillServices()

Returns true if the calling application provides a AutofillService that is enabled for the current user, or false otherwise.

boolean isAutofillSupported()

Returns true if autofill is supported by the current device and is supported for this user.

boolean isEnabled()

Checks whether autofill is enabled for the current user.

void notifyValueChanged(View view)

Called to indicate the value of an autofillable View changed.

void notifyValueChanged(View view, int virtualId, AutofillValue value)

Called to indicate the value of an autofillable virtual view has changed.

void notifyViewEntered(View view)

Called when a View that supports autofill is entered.

void notifyViewEntered(View view, int virtualId, Rect absBounds)

Called when a virtual view that supports autofill is entered.

void notifyViewExited(View view)

Called when a View that supports autofill is exited.

void notifyViewExited(View view, int virtualId)

Called when a virtual view that supports autofill is exited.

void notifyViewVisibilityChanged(View view, int virtualId, boolean isVisible)

Called when a virtual view's visibility changed.

void notifyViewVisibilityChanged(View view, boolean isVisible)

Called when a view's visibility changed.

void registerCallback(AutofillManager.AutofillCallback callback)

Registers a AutofillManager.AutofillCallback to receive autofill events.

void requestAutofill(View view)

Explicitly requests a new autofill context.

void requestAutofill(View view, int virtualId, Rect absBounds)

Explicitly requests a new autofill context for virtual views.

void unregisterCallback(AutofillManager.AutofillCallback callback)

Unregisters a AutofillManager.AutofillCallback to receive autofill events.

Inherited methods

From class java.lang.Object

Constants

EXTRA_ASSIST_STRUCTURE

added in API level 26
String EXTRA_ASSIST_STRUCTURE

Intent extra: The assist structure which captures the filled screen.

Type: AssistStructure

Constant Value: "android.view.autofill.extra.ASSIST_STRUCTURE"

EXTRA_AUTHENTICATION_RESULT

added in API level 26
String EXTRA_AUTHENTICATION_RESULT

Intent extra: The result of an authentication operation. It is either a fully populated FillResponse or a fully populated Dataset if a response or a dataset is being authenticated respectively.

Type: FillResponse or a Dataset

Constant Value: "android.view.autofill.extra.AUTHENTICATION_RESULT"

EXTRA_CLIENT_STATE

added in API level 26
String EXTRA_CLIENT_STATE

Intent extra: The optional extras provided by the AutofillService.

For example, when the service responds to a onSuccess(android.service.autofill.FillResponse) with a FillResponse that requires authentication, the Intent that launches the service authentication will contain the Bundle set by setClientState(Bundle) on this extra.

Type: Bundle

Constant Value: "android.view.autofill.extra.CLIENT_STATE"

Public methods

cancel

added in API level 26
void cancel ()

Called to indicate the current autofill context should be cancelled.

This method is typically called by Views that manage virtual views; for example, when the view is rendering an HTML page with a form and virtual views that represent the HTML elements, it should call this method if the user does not post the form but moves to another form in this page.

Note: This method does not need to be called on regular application lifecycle methods such as finish().

commit

added in API level 26
void commit ()

Called to indicate the current autofill context should be commited.

This method is typically called by Views that manage virtual views; for example, when the view is rendering an HTML page with a form and virtual views that represent the HTML elements, it should call this method after the form is submitted and another page is rendered.

Note: This method does not need to be called on regular application lifecycle methods such as finish().

disableAutofillServices

added in API level 26
void disableAutofillServices ()

If the app calling this API has enabled autofill services they will be disabled.

hasEnabledAutofillServices

added in API level 26
boolean hasEnabledAutofillServices ()

Returns true if the calling application provides a AutofillService that is enabled for the current user, or false otherwise.

Returns
boolean

isAutofillSupported

added in API level 26
boolean isAutofillSupported ()

Returns true if autofill is supported by the current device and is supported for this user.

Autofill is typically supported, but it could be unsupported in cases like:

  1. Low-end devices.
  2. Device policy rules that forbid its usage.

Returns
boolean

isEnabled

added in API level 26
boolean isEnabled ()

Checks whether autofill is enabled for the current user.

Typically used to determine whether the option to explicitly request autofill should be offered - see requestAutofill(View).

Returns
boolean whether autofill is enabled for the current user.

notifyValueChanged

added in API level 26
void notifyValueChanged (View view)

Called to indicate the value of an autofillable View changed.

Parameters
view View: view whose value changed.

notifyValueChanged

added in API level 26
void notifyValueChanged (View view, 
                int virtualId, 
                AutofillValue value)

Called to indicate the value of an autofillable virtual view has changed.

Parameters
view View: the virtual view parent.

virtualId int: id identifying the virtual child inside the parent view.

value AutofillValue: new value of the child.

notifyViewEntered

added in API level 26
void notifyViewEntered (View view)

Called when a View that supports autofill is entered.

Parameters
view View: View that was entered.

This value must never be null.

notifyViewEntered

added in API level 26
void notifyViewEntered (View view, 
                int virtualId, 
                Rect absBounds)

Called when a virtual view that supports autofill is entered.

The virtual view boundaries must be absolute screen coordinates. For example, if the parent, non-virtual view uses bounds to draw the virtual view inside its Canvas, the absolute bounds could be calculated by:

   int offset[] = new int[2];
   getLocationOnScreen(offset);
   Rect absBounds = new Rect(bounds.left + offset[0],
       bounds.top + offset[1],
       bounds.right + offset[0], bounds.bottom + offset[1]);
 

Parameters
view View: the virtual view parent.

This value must never be null.

virtualId int: id identifying the virtual child inside the parent view.

absBounds Rect: absolute boundaries of the virtual view in the screen.

This value must never be null.

notifyViewExited

added in API level 26
void notifyViewExited (View view)

Called when a View that supports autofill is exited.

Parameters
view View: View that was exited.

This value must never be null.

notifyViewExited

added in API level 26
void notifyViewExited (View view, 
                int virtualId)

Called when a virtual view that supports autofill is exited.

Parameters
view View: the virtual view parent.

This value must never be null.

virtualId int: id identifying the virtual child inside the parent view.

notifyViewVisibilityChanged

added in API level 27
void notifyViewVisibilityChanged (View view, 
                int virtualId, 
                boolean isVisible)

Called when a virtual view's visibility changed.

Parameters
view View: View that was exited.

This value must never be null.

virtualId int: id identifying the virtual child inside the parent view.

isVisible boolean: visible if the view is visible in the view hierarchy.

notifyViewVisibilityChanged

added in API level 27
void notifyViewVisibilityChanged (View view, 
                boolean isVisible)

Called when a view's visibility changed.

Parameters
view View: View that was exited.

This value must never be null.

isVisible boolean: visible if the view is visible in the view hierarchy.

registerCallback

added in API level 26
void registerCallback (AutofillManager.AutofillCallback callback)

Registers a AutofillManager.AutofillCallback to receive autofill events.

Parameters
callback AutofillManager.AutofillCallback: callback to receive events.

This value may be null.

requestAutofill

added in API level 26
void requestAutofill (View view)

Explicitly requests a new autofill context.

Normally, the autofill context is automatically started if necessary when notifyViewEntered(View) is called, but this method should be used in the cases where it must be explicitly started. For example, when the view offers an AUTOFILL option on its contextual overflow menu, and the user selects it.

Parameters
view View: view requesting the new autofill context.

This value must never be null.

requestAutofill

added in API level 26
void requestAutofill (View view, 
                int virtualId, 
                Rect absBounds)

Explicitly requests a new autofill context for virtual views.

Normally, the autofill context is automatically started if necessary when notifyViewEntered(View, int, Rect) is called, but this method should be used in the cases where it must be explicitly started. For example, when the virtual view offers an AUTOFILL option on its contextual overflow menu, and the user selects it.

The virtual view boundaries must be absolute screen coordinates. For example, if the parent view uses bounds to draw the virtual view inside its Canvas, the absolute bounds could be calculated by:

   int offset[] = new int[2];
   getLocationOnScreen(offset);
   Rect absBounds = new Rect(bounds.left + offset[0],
       bounds.top + offset[1],
       bounds.right + offset[0], bounds.bottom + offset[1]);
 

Parameters
view View: the virtual view parent.

This value must never be null.

virtualId int: id identifying the virtual child inside the parent view.

absBounds Rect: absolute boundaries of the virtual view in the screen.

This value must never be null.

unregisterCallback

added in API level 26
void unregisterCallback (AutofillManager.AutofillCallback callback)

Unregisters a AutofillManager.AutofillCallback to receive autofill events.

Parameters
callback AutofillManager.AutofillCallback: callback to stop receiving events.

This value may be null.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)