nikib3ro
nikib3ro

Reputation: 20606

Turn off autosuggest for EditText?

Is there a way to programmatically turn off that autosuggest list which pops up as you type in EditText?

Upvotes: 141

Views: 118981

Answers (18)

khcpietro
khcpietro

Reputation: 1539

I have tried every answers in this thread, but had no luck. Here is my tricky workaround.

android:imeOptions="actionSearch" or android:imeOptions="actionSend"

In fact, I needed to autofill my custom items when AutoCompleteTextView focused which is blank, but Android password manager(1password in my case) overrode my custom items immediately.

Upvotes: 0

FawwazFaisal
FawwazFaisal

Reputation: 77

In Kotlin, following snippet worked for me

fun EditText.disablePasting(){
    isLongClickable = false
    inputType = inputType or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
    doBeforeTextChanged { text, start, count, after ->
        if (after - count > 1) {
            setText(this.text)
            setSelection(this.text.toString().length)
        }
    }
}

Then you call your method for each edittext view in the hierarchy as:

private fun disablePasting(view: View?){
    if(view is ViewGroup){
        view.children.forEach {
            if(it is EditText){
                it.disablePasting()
            }else{
                if(it is ViewGroup){
                    disablePasting(it)
                }
            }
        }
    }
}

When InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD is used without InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS, it will still show the suggestion bar on Gboard with the user credentials and selecting a credential would paste the clear password in the field which would be a huge security risk

Upvotes: 0

Dmitry Velychko
Dmitry Velychko

Reputation: 1535

Find a good solution for auto suggestions with TextWatcher. The suggestions will appear for user, but user will not be able accept them.

/*...*/

editText.addTextChangedListener(textWatcher);

/*...*/
}

private final TextWatcher textWatcher = new TextWatcher() {
    
    long timestamp = 0L;
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!longClickable && count - before > 1 && timestamp < System.currentTimeMillis()) { // disable suggestions click
            String str = s.toString();
            str = str.substring(0, before);
            timestamp = System.currentTimeMillis() + 20;
            inputText.setText(str);
            inputText.setSelection(inputText.getText().length());
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
};

Also this works good when you don't need a long click with copy/paste functionality

Upvotes: 0

Saljith Kj
Saljith Kj

Reputation: 145

if you don't want suggestion you can use "none"

android:inputType="none|numberDecimal"

Upvotes: 0

Rajesh N
Rajesh N

Reputation: 6693

This worked for me

android:importantForAutofill="no" 
android:inputType="none|textNoSuggestions"

Upvotes: 12

Enzokie
Enzokie

Reputation: 7415

Ways to do it:

  1. Programatically
editText.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
  1. XML
android:inputType="textNoSuggestions"

If you already have some flags set, then:

  1. Programatically
inputType = if (disableSuggestions) {
  InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS or inputType
} else {
  inputType and InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS.inv()
}
  1. XML
android:inputType="textNoSuggestions|textPassword"

Upvotes: 48

Maxime Mbabele
Maxime Mbabele

Reputation: 91

    // 100% correct & tested
    txtKey = (EditText) view.findViewById(R.id.txtKey);
    txtKey.setPrivateImeOptions("nm");
    txtKey.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        txtKey.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
    }

Upvotes: 4

Mahesh Keshvala
Mahesh Keshvala

Reputation: 1355

For the android version 8.0 or above this code is not working and for the autocomplete textview also this code not working so i will suggest you to use below code for the Disable the auto suggestions in 8.0 or above android vesrion use this property of edittext android:importantForAutofill="no"

                   <EditText
                            android:id="@+id/name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:enabled="false"
                            android:focusable="false"
                            android:imeOptions="actionDone"
                            android:maxLength="80"
                            android:maxLines="1"
                            android:textColor="@color/colorBlack"
                            android:textColorHint="@color/colorBlack"
                            android:importantForAutofill="no"
                            android:textSize="@dimen/_12sdp"
                            app:bFontsEdt="light" />

and for the AutoComplete textview like this use this Field to Disable Auto suggestion android:importantForAutofill="no" :

      <AutoCompleteTextView
                        android:id="@+id/autocompleteEditTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/_3sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:background="@null"
                        android:imeOptions="actionDone"
                        android:singleLine="true"
                        android:text=""
                        android:textColor="@color/colorBlack"
                        android:textColorHint="@color/colorGrayDark"
                        android:importantForAutofill="no"
                        android:textSize="@dimen/_12sdp" />

Upvotes: 29

nikib3ro
nikib3ro

Reputation: 20606

OK, the problem was - in Eclipse you don't get suggestion for flag named: textNoSuggestion

And you can't set it in main.xml (where you design UI) because that attribute for inputType isn't recognized. So you can set it in code using int const:

EditText txtTypeIt = (EditText) this.findViewById(R.id.txtTypeIt);
txtTypeIt.setInputType(524288);

And thanks jasta00 for helping me out figure answer for this one.

Upvotes: 4

Ajji
Ajji

Reputation: 3086

I was getting this thing on samsung phones running 4.4.4. This solved my problem

android:inputType="text|textVisiblePassword|textNoSuggestions"

Upvotes: 11

Tariq
Tariq

Reputation: 593

As this is still an issue, I'm going to post this answer. android:inputType="textVisiblePassword" works but it is undesirable, first because it's a misrepresentation of the input and second because it disables Gesture Typing!

Instead I had to use BOTH flags textNoSuggestions and textFilter. Either one alone did not work.

android:inputType="textCapSentences|textFilter|textNoSuggestions"

The textCapSentences was for other reasons. But this inputType kept the swipe typing and disabled the suggestion toolbar.

Upvotes: 7

Michael Peterson
Michael Peterson

Reputation: 10532

android:inputType="text|textNoSuggestions"

Upvotes: 3

Mr_Hmp
Mr_Hmp

Reputation: 2535

android:inputType="textVisiblePassword" 

works like a charm

Upvotes: 71

Carl
Carl

Reputation: 15605

The most reliable approach I have found to getting rid of autocomplete is to use

InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD 

on your EditText control. As charlie has reported in a different answer on this page,

android:inputType="textVisiblePassword"

is the XML version of this flag.

You can combine this flag with

InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

I had been using InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS without InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, which worked for most phones, but then I came across a Samsung phone for which I was still getting autocomplete.

Android programmatically disable autocomplete/autosuggest for EditText in emulator

which suggested using InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.

I tried this (along with InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) and it worked. You can see why even a phone that might not take the hint that you don't want autocomplete would have to allow it to be disabled for a password field. Short of holding our breath, this might be the best way to get what we want from such phones.

On some of my devices, the font was slightly changed by this flag - most noticeably to distinguish a zero (0) from an Oh (O) more clearly, which obviously would be important for displaying a password. But at least it worked, and the new font was not unattractive.

Even though the post on which I found this suggestion was old, the phone I tested on was very recent - a Samsung Galaxy Note II (SPH-L900) stock Android 4.1.2 from Sprint. The keyboard selected was "Samsung Keyboard," and apparently this was the default for this phone when the customer received it from Sprint. So this problem apparently has persisted over the years for at least some of the important Samsung line of phones.

For those of you who, like me, do not have a Samsung test device, this could be important information.

Upvotes: 24

kaolick
kaolick

Reputation: 4857

You could simply use the EditText's setThreshold() method. Set the threshold to let's say 100 when you don't want to show predictions. If you want to re-activate showing predictions, set it back to a small int like 1 or 2 depending on your needs.

Upvotes: 0

Thomas V J
Thomas V J

Reputation: 658

EditText emailTxt=(EditText)findViewById(R.id.email);
emailTxt.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

This will help you.

Upvotes: 1

k4dima
k4dima

Reputation: 6251

android:inputType="textNoSuggestions"  

also you'd better read this

Upvotes: 127

glr
glr

Reputation: 2147

I had the same question but I still wanted to set this option in my XML file so I did a little more research until I found it out myself.

Add this line into your EditText.

android:inputType="textFilter" 

Here is a Tip. Use this line if you want to be able to use the "enter" key.

android:inputType="textFilter|textMultiLine"

Upvotes: 133

Related Questions