Schnapse
Schnapse

Reputation: 483

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.

Upvotes: 3

Views: 4383

Answers (2)

Jay makwana
Jay makwana

Reputation: 97

Insert below onCreate->setContentView():

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

Upvotes: -1

CzarMatt
CzarMatt

Reputation: 1833

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);

Upvotes: 6

Related Questions