user995009
user995009

Reputation:

Programmatically created EditText's Input Type Password not working

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

Answers (6)

Tara
Tara

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

Jeffy Lazar
Jeffy Lazar

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

lakshay
lakshay

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

Harshad Prajapati
Harshad Prajapati

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:

enter image description here

Upvotes: 0

Nikunj
Nikunj

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

Shivam Oberoi
Shivam Oberoi

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

Related Questions