chrisjenx / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
8.59k stars 1.09k forks source link

Password Hint text does not have correct font applied #186

Open mattinger opened 9 years ago

mattinger commented 9 years ago

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.

Sanjay-F commented 9 years ago

v2.1.0

when i use

etPasword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

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

chrisjenx commented 9 years ago

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

fingertricks commented 9 years ago

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

...then Android applies a Monospace typeface.

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

...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());
        }
    });
chrisjenx commented 9 years ago

Wow nice work.

On Mon, 7 Sep 2015 21:52 Dave Jones notifications@github.com wrote:

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

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 .

Sanjay-F commented 9 years ago

@fingertricks thxxx very much!

fingertricks commented 9 years ago

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

marcforn commented 9 years ago

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

saqada commented 8 years ago

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

chrisjenx commented 8 years ago

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

saqada commented 8 years ago

@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);
cdmunoz commented 8 years ago

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

Honey2504 commented 8 years ago

Will InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS also ignore the setTypeface?

davweb commented 8 years ago

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);
chrisjenx commented 8 years ago

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 .

QiiqeAzuara commented 8 years ago

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

Variag13 commented 6 years ago

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