1

How can you set AppCompatEditText as inputType textPassword programatically?

I have tried all possible methods to solve this:

pass_et = (AppCompatEditText) loginView.findViewById(R.id.pass_et);
     pass_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            pass_et.setSelection(pass_et.getText().length());
            pass_et.setTransformationMethod(PasswordTransformationMethod.getInstance());
            pass_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

    pass_et.setTypeface(Typeface.DEFAULT);
            pass_et.setTransformationMethod(new PasswordTransformationMethod());

     pass_et.setTypeface(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Lato-Italic.ttf"));       pass_et.setTransformationMethod(new PasswordTransformationMethod());

    pass_et.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                     pass_et.setTypeface(Typeface.DEFAULT);
            pass_et.setTransformationMethod(new PasswordTransformationMethod());
                    pass_et.setInputType(

                            InputType.TYPE_TEXT_VARIATION_PASSWORD);

                    pass_et.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            });

I have also tried:

pass_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
mEdit.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Chandu
  • 21
  • 2
  • 2
    Possible duplicate of [Set inputType for an EditText?](https://stackoverflow.com/questions/2586301/set-inputtype-for-an-edittext) – Zainab Jamil Mar 08 '18 at 12:35
  • this might help https://stackoverflow.com/questions/3685790/how-to-switch-between-hide-and-view-password – krishank Tripathi Mar 08 '18 at 12:50
  • pass_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); mEdit.setTransformationMethod(PasswordTransformationMethod.getInstance()); all i tried but there is no use, if i add **textPassword** in xml file it is working but font is changing, its different from the font i applied in app – Chandu Mar 09 '18 at 04:31
  • You can use the [edit] button to make changes to your questions to include extra information. – Martin Evans Mar 10 '18 at 12:47
  • Your question is wrongly put. Setting input type works but you lose typeface, is that correct? Edit your question accordingly. – Eugen Pechanec Mar 10 '18 at 13:12

2 Answers2

1

Don't call setTransfromationMethod(PasswordTransformationMethod.getInstance()), it's done as part of setInputType. Ctrl+click the method name to see source code.

When you set input type to password, typeface is automatically set to monospace. So, set typeface after setInputType.

And since you already need to call setTypeface after everything is set up, you might as well use XML attributes. Then set typeface in code.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

I solved my problem by this:

doOnTextChanged { text, start, count, after ->
            inputType = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
            setSelection(text?.length ?: 0)
}
JQueue
  • 1