Search code examples
androidinputdialogpasswordsautosuggest

Android | How disable auto-suggestion of an input programmatically?


In one of my application I'm trying to put a password confirmation in a dialog. The dialog is entirely build programmatically. I had some problem to set the type of the input to password but it's working. Later, I just noticed that the keyboard has the auto-suggestion option.

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(...); 
builder.setIcon(...); 
builder.setMessage(...); 
builder.setCancelable(false); 

EditText etForcePassword = new EditText(mContext); 
etForcePassword.setSingleLine(); 
etForcePassword.setHint(...);
etForcePassword.setImeOptions(EditorInfo.IME_ACTION_DONE);
etForcePassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
etForcePassword.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS | EditorInfo.TYPE_TEXT_VARIATION_FILTER); 
builder.setView(etForcePassword, top, right, bottom, left); 

AlertDialog alertDialog = builder.create(); 
alertDialog.show();`

I saw a lot of solution by setting the InputType or simply using XML configuration but it's not fitting my application.

Thanks in advance.


Solution

  • To turn off auto-suggestion, change this line:

    etForcePassword.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS | EditorInfo.TYPE_TEXT_VARIATION_FILTER);
    

    to this line:

    etForcePassword.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);