Disabling Android O auto-fill service for an application
Asked Answered
W

10

46

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 ?

Watersoak answered 17/8, 2017 at 9:29 Comment(5)
Autofill can cause a crash that is really obscure, occurs for us when a user changes focus to a previously filled textInputEditText inside a TextInputLayout: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object referenceVibrato
fired a bug issuetracker.google.com/issues/67675432, please star itAre
Create an activity class that is used for every other activity to inherit and add this code to it.Conah
@Justin, This is fixed on Android 8.1, can you test it with 8.1Perr
I believe it can also cause TransactionTooLargeExceptions if it tries to autofill something like an EditText with > 1MB of data, which exceeds the limit of a Bundle. See the source for AutoFillManager. Workaround is to include android:importantForAutofill="noExcludeDescendants" in the View's xml.Osier
B
3

Is it possible ?

Not that I am aware of. Certainly, nothing is documented.

Is there any better method than this ?

Not that I am aware of.

Beyond answered 17/8, 2017 at 10:49 Comment(3)
This is fixed on Android 8.1, can you test it with 8.1Perr
Is it possible android:importantForAutofill="no"Greeley
@appnweb31web: That is an attribute for views, not for an <application> in the manifest.Beyond
E
30

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!

Election answered 6/9, 2017 at 19:25 Comment(4)
very nice! I'm going to mix this answer with API checking for Android 8.1; disable the bugged autofill in 8.0 but allow it in 8.1+. Otherwise I would spend over an hour putting the hint into TextInputLayout throughout the app.Embree
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS); }Embree
this answer doesn't work on Emulator (API 26): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object referenceEmbree
I can't get any effect of getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS) on Android 10.Viaduct
V
26

I believe the accepted answer is incorrect:

So I have my own class which is extends the android.support.v7.widget.AppCompatEditText and all I did is overwrote the following method with the following value:

@Override
public int getAutofillType() {
    return AUTOFILL_TYPE_NONE;
}

no other solutions worked, not even android:importantForAutofill="no".

getAutofillType() comes from the View class, so it should work for every other class such as TextInputEditText too!

Valtin answered 11/10, 2017 at 21:43 Comment(7)
This is the only working solution for now, it works good for TextInputEditText too!Are
Added code example to showcase repo github.com/BukT0p/AutofillBugAre
If you use any third party library with visual components that don't extend your base class, this won't workFontana
I confirm that this solution worked, while setting android:importantForAutofill="no" or android:importantForAutofill="noExcludeDescendants" on the EditText did not work. Tested on Oreo 8.0 (api level 26) and Android Pie (api level 28) emulatorsWharfage
Even though annoying to implement, this should be the accepted answerBranch
Its 2022 and this is the only solution that works.Petrel
This must be the accepted answer. Its been weeks I was stuck on this API to save custom passwords. Only this approach worked for me.Tanishatanitansy
B
12

I ran into this too. It turns out the issue was caused by setting the hint text on the EditText nested inside the TextInputLayout.

I did some digging and found this nugget in the 26.0.0 Beta 2 release notes. Andorid Support Release Notes June 2017

TextInputLayout must set hints on onProvideAutofillStructure()

That led me to try setting the hint on the TextInputLayout instead of the nested EditText.

This resolved the crashing issue for me. Example:

            <android.support.design.widget.TextInputLayout
                android:id="@+id/textInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Some Hint Text"
                android.support.design:hintAnimationEnabled="true"
                android.support.design:hintEnabled="true"
                android.support.design:layout_marginTop="16dp">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </android.support.design.widget.TextInputLayout>
Boom answered 30/10, 2017 at 17:52 Comment(4)
It solves only TextInputEditText bug, updated my repo(github.com/BukT0p/AutofillBug) with your solution. But there was similar bug for AutoCompleteTextView. In API 27 autofill works in sync mode and causes UI freezes and lags. So maybe it's better to disable autofill for views that should not be filled for sureAre
Even if we use the other suggestions in this thread, to disable auto-fill, the user can still long press on a view and select auto-fill from the context menu. I have observed that in some cases you will be told a view cannot be autofilled, but I have not found that flag yet.I have seen some solutions that involve a custom view that returns NONE for autofill options, but that is not feasible for all implementations.Boom
This solutions works for me. User can still long-click the input field and select autofill but it doesnt crash - it just do nothing. Thanks.Sue
This approach works, but was the biggest PITA because my app has tons of activities. Note, I didn't use the lines starting with android.support.design just moving the hint parameter is all that's needed.Embree
T
4

Seems to be a bug that needs to be fixed : https://issuetracker.google.com/issues/67675432
In the meanwhile a workaround for now is to disable the AutoFill feature for the whole project. You can add in the values-v26/styles.xml file the following style or you can edit your BaseEditTextStyle if you are using a specific style for your EditText views.

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
<item name="android:importantForAutofill">noExcludeDescendants</item>
</style>

and in the values-v26/themes.xml file you can simply add to the default theme that you are using in your app the items editTextStyle and android:editTextStyle like following :

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:editTextStyle">@style/App_EditTextStyle</item>
<item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

this way you can apply this changes for all your EditTexts without needing to change your layout files or Activities (and later on you can easily remove it when the bug is fixed).

Torquemada answered 20/10, 2017 at 16:45 Comment(0)
B
3

Is it possible ?

Not that I am aware of. Certainly, nothing is documented.

Is there any better method than this ?

Not that I am aware of.

Beyond answered 17/8, 2017 at 10:49 Comment(3)
This is fixed on Android 8.1, can you test it with 8.1Perr
Is it possible android:importantForAutofill="no"Greeley
@appnweb31web: That is an attribute for views, not for an <application> in the manifest.Beyond
R
2

Create custom EditText style and set android:importantForAutofill to no.

<style name="EditTextStyleWithoutAutoFill" parent="Widget.AppCompat.EditText">
    <item name="android:importantForAutofill">no</item>
</style>

Then in your activity theme set this style for editTextStyle.

<item name="android:editTextStyle">@style/EditTextStyleWithoutAutoFill</item>
Richia answered 19/8, 2017 at 17:46 Comment(2)
importantForAutofill documented here developer.android.com/guide/topics/text/…Archetype
I'll try this approach next, but merge it with res/values-v26/styles.xml to only affect Android 8.0, not 8.1 (I can' tbelieve Google released such a gigantic bug!)Embree
C
2

In your EditText attributes add android:importantForAutofill="no" This should be a temporary fix and will only apply to api 26+

Clichy answered 10/11, 2017 at 6:38 Comment(1)
how this is different from other answers?Are
T
2

In my case, our app targets SDK version 21 but newer devices (26+) were still popping up the autocomplete. Pretty big problem if the app runs on devices that are shared between people. Using just android:importantForAutofill="no" did not work for me.

The only solution that I found to work in my case was:

<EditText
    android:importantForAutofill="no"
    tools:targetApi="o"
    android:autofillHints="AUTOFILL_HINT_SMS_OTP" ...

The reason I added android:autofillHints="AUTOFILL_HINT_SMS_OTP" was because if you long-pressed on the EditText it would still bring up autofill. Basically, I told the field's autofill that it is waiting for a text message that will never be sent. Bit of a hack, I know...

Note: you may have to add xmlns:tools="http://schemas.android.com/tools" to your schemas' if it is not there already.

True answered 9/1, 2020 at 23:55 Comment(2)
That works for me. Did it cause you a problem on lower APIs?Moncear
I can't speak to anything below Lollipop, haven't tested. But for Lollipop to latest, haven't had any issues since this was posted.True
H
1

Had the same problem with API 28+ and disable Autofill. For me the only solution was to disable long click for my views.

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:longClickable="false"
                    android:text="@={model.emailAdress}"/>
Henri answered 10/1, 2020 at 1:38 Comment(0)
P
0

With reference to Google issue tracker, it has been fixed.

This is fixed on Android 8.1

If any issue persists, please report at Google issue tracker they will re-open to examine.

Perr answered 3/1, 2018 at 9:0 Comment(1)
This answer has nothing to do with the question. It does have something to do with the first comment on the question.Beyond

© 2022 - 2024 — McMap. All rights reserved.