1

I have an edit text and on long press on it i dont want autofill option to be included along side copy ,paste ,select etc

I tried doing the below code which in turn didn't help me

if (Build.VERSION.SDK_INT >= 26) {
            AutofillManager autofillManager = cxt.getSystemService(AutofillManager.class);
            if (null != autofillManager) {
                view.setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
                autofillManager.disableAutofillServices();
                autofillManager.cancel();
            }
        }

I expected Autofill option should not be listed but it is listed along with copy ,paste ,select etc

AskNilesh
  • 67,701
  • 16
  • 123
  • 163

2 Answers2

0

You can do this by custom EditText class just need to below steps :

1) Extend the EditText class,

2) Override isSuggestionsEnabled() and return false,

3) Create a canPaste() method and return false. This is method hiding.

If still not work you can disable the long-click in the EditText as well.

mEditText.setLongClickable(false);

And disable the context menus from appearing by returning false from these methods:

mEditEext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public void onDestroyActionMode(ActionMode mode) {                  
            }

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        });
Vishal G. Gohel
  • 1,008
  • 1
  • 16
  • 31
0

The answer is almost exactly the same as what I give here: https://stackoverflow.com/a/59870250/7729375

The only difference is the targeted Id of the removeItem is now android.R.id.autofill and that it requires the annotation for API 26 (Android O)

Java:

ActionMode.Callback callback = new ActionMode.Callback() {
            @Override
            @TargetApi(26)
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                if (menu != null) {
                    menu.removeItem(android.R.id.autofill);
                }
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        };

        mEditText.setCustomInsertionActionModeCallback(callback);

        mEditText.setCustomSelectionActionModeCallback(callback);

Kotlin:

val callback = object : ActionMode.Callback {
    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
        return false
    }

    @TargetApi(Build.VERSION_CODES.O)
    override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
        menu?.removeItem(android.R.id.autofill)
        return true
    }

    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
        return true
    }

    override fun onDestroyActionMode(mode: ActionMode?) {}
}

then for use site in EditText :

fun preventPaste() {
    customInsertionActionModeCallback = callback
    customSelectionActionModeCallback = callback
}
Benjamin Charais
  • 1,248
  • 8
  • 17