0

I am trying to use a custom font in a password hint field in Android Studio.

After I set the Edit Text as inputType="textPassword" in XML, the font changes to a different from the one I've chosen. All the other Edit Texts have the correct custom font.

I've tried following this solution in java code (replaced Typeface.DEFAULT with R.font.my_font) but nothing happened

    EditText password = (EditText) findViewById(R.id.register_password_text);
    password.setTypeface(R.font.my_font);
    password.setTransformationMethod(new PasswordTransformationMethod());

I've also tried this solution with no success

    Typeface cache = edtPassword.getTypeface();
    edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    edtPassword.setTypeface(cache);

Any ideas on what to try or what might be missing in these solutions?

PS: I'm using montserrat_regular font

Tiago Santos
  • 726
  • 6
  • 13

1 Answers1

0

Its happening because you are passing the id of the font in password.setTypeface(R.font.my_font) not the TypeFace Object. You need to pass TypeFace object in setTypeFace() method , so to make TypeFace object from your font resource Id just add the code

Typeface typeface = ResourcesCompat.getFont(this, R.font.my_font);
password.setTypeface(typeface);

I think this should work.