Disabling Android O auto-fill service for an application

jgm picture jgm · Aug 17, 2017 · Viewed 24.6k times · Source

Android O has the feature to support Auto-filling for fields. Is there any way I can disable it for a specific application. That is I want to force my application not to use the auto-fill service.

Is it possible ?

To block autofill for an entire activity, use this in onCreate() of the activity:

getWindow()
  .getDecorView()
  .setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);

Is there any better method than this ?

Answer

albeee picture albeee · Sep 6, 2017

Currently there is no direct way to disable the autofill for an entire application, since the autofill feature is View specific.

You can still try this way and call BaseActivity everywhere.

public class BaseActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       disableAutofill();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void disableAutofill() { 
        getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
    }
}

You can also force request autofill this way.

public void forceAutofill() {
    AutofillManager afm = context.getSystemService(AutofillManager.class);
    if (afm != null) {
        afm.requestAutofill();
    }
}

Note: At the moment autofill feature is only available in API 26 Android Oreo 8.0

Hope this helps!