Calligraphy: Password Hint text does not have correct font applied

If i try to use calligraphy for a password field as such:

<EditText
            style="@style/AppTheme.Font.Lgt"
            android:id="@+id/passwordText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimens/text_size_med"
            android:hint="@string/password_hint"
            android:inputType="textPassword"
            android:text=""/>

It seems that the inputType attribute is forcing the hint into being monospaced (which seems to be odd, but normal android behavior with EditText). In this case, the font being applied by the style is ignored. Presumably because it is applied prior to android applying the inputType, and thus the typeface is getting overriden.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 4
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@here @mattinger I try this way:

1.- remove android:inputType=“textPassword” from xml 2.- apply typeface using this great library @chrisjenx thanks 3.- set passwordtransformation in code: password.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); // No suggestions password.setTransformationMethod(PasswordTransformationMethod.getInstance());

DONE !!

My solution is to save the Typeface before setting the input type and put it back afterwards.

Typeface typeface = editText.getTypeface();
editText.setInputType(inputType);
editText.setTypeface(typeface);

@Sanjay-F if you use TextView.setInputType() with any of:

  • InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
  • InputType.TYPE_TEXT_VARIATION_PASSWORD
  • InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD
  • InputType.TYPE_NUMBER_VARIATION_PASSWORD

…then Android applies a Monospace typeface.

Note: you need to bitwise OR the above variations with the appropriate:

  • InputType.TYPE_CLASS_TEXT
  • InputType.TYPE_CLASS_NUMBER

…when calling TextView.setInputType() otherwise Android doesn’t change the transformation type.

If for example you are using a button to toggle a password to be visible or not, you’ll have to manually set the Typeface after each call to setInputType() if you want to apply a custom Typeface. When toggling password visibility Android appears to reset the cursor position to the start of the EditText, so this snippet also sets the cursor to the end of the EditText.

mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
            int type = checked ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_TEXT_VARIATION_PASSWORD;
            mPassword.setInputType(InputType.TYPE_CLASS_TEXT | type);
            mPassword.setTypeface(customTypeface);
            mPassword.setSelection(mPassword.length());
        }
    });

Setting the type face from another text field of the form, i.e the user name type face is the easiest and simple way to do this: passwordField.setTypeface(usernameField.getTypeface());

@QiiqeAzuara your solution with InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS caused autocomplete to show up and it’s not safe IMHO. I tried with InputType.TYPE_TEXT_VARIATION_PASSWORD and it worked - both TextInputLayout and EditText use custom font, password is not visible and there is no autocomplete.

The work around is the easy bit it’s all the hacky work arounds for every single weird view that I can’t support.

On Thu, 6 Oct 2016, 07:16 davweb, notifications@github.com wrote:

My solution is to save the Typeface before setting the input type and put it back afterwards.

Typeface typeface = editText.getTypeface(); editText.setInputType(inputType); editText.setTypeface(typeface);

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/186#issuecomment-251876299, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRsVrLzTzfqdkgBDYbeNlLcgiNCv9Nks5qxJJQgaJpZM4FOV-W .

@chrisjenx thank you, I didn’t know it. So, now I can replace TypefaceSpan typefaceSpan = new CustomTypefaceSpan(face); by CalligraphyTypefaceSpan typefaceSpan = TypefaceUtils.getSpan(face); No need to create CustomTypefaceSpan as it is done on the link I provided before.

Final code to support a custom font for password EditText is:

Typeface face = Typeface.createFromAsset(getAssets(), CalligraphyConfig.get().getFontPath());

EditText passwordEdittext = new EditText(getApplicationContext());
passwordEdittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordEdittext.setTypeface(face, Typeface.ITALIC);  // or Typeface.NORMAL or any other

CalligraphyTypefaceSpan typefaceSpan = TypefaceUtils.getSpan(face);
SpannableString spannableString = new SpannableString("Hint text");
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

passwordEdittext.setHint(spannableString);