Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Password Hint text does not have correct font applied

Open mattinger opened this issue 9 years ago • 16 comments

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.

mattinger avatar Jun 29 '15 18:06 mattinger

v2.1.0

when i use

etPasword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

i meet the same problem help...any body. ;(

Sanjay-F avatar Sep 06 '15 10:09 Sanjay-F

@mattinger @Sanjay-F I haven't had a chance to look into this. I would guess that setTypeface does not apply to the password texts, but not sure.

chrisjenx avatar Sep 07 '15 18:09 chrisjenx

@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());
        }
    });

fingertricks avatar Sep 07 '15 20:09 fingertricks

Wow nice work.

On Mon, 7 Sep 2015 21:52 Dave Jones [email protected] wrote:

@Sanjay-F https://github.com/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])( http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/widget/TextView.java#TextView.setInputType%28int%29) 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()); } });

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/186#issuecomment-138376055 .

chrisjenx avatar Sep 07 '15 22:09 chrisjenx

@fingertricks thxxx very much!

Sanjay-F avatar Sep 08 '15 02:09 Sanjay-F

@Sanjay-F you're welcome! I had solved that issue myself just last week.

In fact since Monday I found another tweak: you can call getSelectionStart() prior to the call to setInputType() and then use that to restore the cursor position with setSelection(). Just check that getSelectionStart() returns >= 0 though. This is preferable to moving the cursor to the end of the password field when toggling visibility.

You might also be able to do something with getTypeface(), but I haven't had time to look into that.

fingertricks avatar Sep 09 '15 21:09 fingertricks

I've found the same problem when using TextInputLayout. I can set the correct Typeface of the EditText but the TextInputLayout remains with monospace.

Do you know any workaround I could take?

Thanks in advance

marcforn avatar Sep 25 '15 22:09 marcforn

For those who still face the same issue: http://stackoverflow.com/a/35322532/1808829

saqada avatar Jun 01 '16 17:06 saqada

@saqada there is a TypefaceSpannable in Calligraphy if you wan't want to write your own .

chrisjenx avatar Jun 02 '16 16:06 chrisjenx

@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);

saqada avatar Jun 03 '16 12:06 saqada

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());

cdmunoz avatar Jun 22 '16 17:06 cdmunoz

Will InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS also ignore the setTypeface?

Honey2504 avatar Sep 06 '16 15:09 Honey2504

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);

davweb avatar Oct 06 '16 06:10 davweb

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, [email protected] 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 avatar Oct 06 '16 14:10 chrisjenx

@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 !!

QiiqeAzuara avatar Oct 11 '16 21:10 QiiqeAzuara

@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.

Variag13 avatar Dec 14 '17 15:12 Variag13