Reputation:
I have created an EditText
programmatically in AlertDialog
but am not able to change its input type to password. Below is my code,
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText edittext = new EditText(ChildMainScreen.this);
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edittext.setSingleLine(true);
alert.setMessage("Please enter password to continue");
alert.setView(edittext);
alert.show();
I have also tried,
edittext.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
and
edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());
But no luck.
Android version is 7 (Nougat)
Upvotes: 2
Views: 2696
Reputation: 700
first add this in your manifest
<activity
android:name=".YourActivity"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />
And:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText edittext = new EditText(ChildMainScreen.this);
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edittext.setSingleLine(true);
edittext.setMaxLines(1);
alert.setMessage("Please enter password to continue");
alert.setView(edittext);
alert.show();
then check it gives you your desired behavior
Upvotes: 0
Reputation: 1923
You can try the below to get password as input
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
final EditText edittext = new EditText(getActivity());
edittext.setRawInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
edittext.setSingleLine(true);
edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());
alert.setMessage("Please enter password to continue");
alert.setView(edittext);
alert.show();
as per the documentation
TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
is entering a password, which should
* be visible to the user.
TYPE_TEXT_VARIATION_PASSWORD
is entering a password.
Upvotes: 0
Reputation: 713
Just replace edittext.setSingleLine(true);
with edittext.setMaxLines(1);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText edittext = new EditText(ChildMainScreen.this);
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edittext.setMaxLines(1);
alert.setMessage("Please enter password to continue");
alert.setView(edittext);
alert.show();
Upvotes: 4
Reputation: 830
Try this:-
Try both thing at same time
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText edittext = new EditText(ChildMainScreen.this);
edittext.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edittext.setSingleLine(true);
edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());
alert.setMessage("Please enter password to continue");
alert.setView(edittext);
alert.show();
Output:
Upvotes: 0
Reputation: 4157
You need to write this:
editText.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
Upvotes: 0
Reputation: 1445
Try this-:
edittext.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);`
Instead of-:
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
Upvotes: 0