Home » Android » android – Setting typeface of hint in TextEdit

android – Setting typeface of hint in TextEdit

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is it possible to set typeface of hint element of EditText view without changing the typeface of the EditText itself? Thanks.

How to&Answers:

Seems that there is no way to control typeface of hint and typeface of text separately.

Answer:

Did you check android:typeface?

This works fine both for the hint and text for the EditText.

Answer:

Don’t know if you just need to have them be different, or if you wanted to specifically set a typeface.

If you just need to style your hint differently than your text, here’s how you can do it in Java on a per-field basis

  1. Wrap your hint text in HTML style tags
  2. Pass the marked-up String to Html.fromHtml(String htmlString)
  3. Pass the result of fromHtml() to setHint();

    myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
    

Answer:

You can set hint font by override typefacefont

public class CustomEditText extends EditText {

private Context context;
private AttributeSet attrs;
private int defStyle;

public CustomEditText(Context context) {
    super(context);
    this.context = context;
    init();
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.attrs = attrs;
    init();
}

public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    this.attrs = attrs;
    this.defStyle = defStyle;
    init();
}

private void init() {
    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "opensansbold.ttf");
    this.setTypeface(font);
}

@Override
public void setTypeface(Typeface tf, int style) {
    tf = Typeface.createFromAsset(getContext().getAssets(), "opensansbold.ttf");
    super.setTypeface(tf, style);
}
}

Answer:

in order to have specific font there is a great library here , its very simple, but to have a edittext with android:inputType=”textPassword” you should do a little more, so here it is:

  1. remove android:inputType=”textPassword” from xml
  2. apply typeface using this library
  3. set passwordtransformation in code:

    password.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    password.setTransformationMethod(PasswordTransformationMethod.getInstance());

Answer:

In case somebody still needs a solution for this:

 editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                editText.typeface = ResourcesCompat.getFont(it, if (TextUtils.isEmpty(s.toString())) R.font.franklingothic_book else R.font.franklingothic_demi)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
        })

Answer:

the best way to handle this problem is to add a TextWatcher to your EditText and dynamically change the typeface when there’s no input in the EditText

Answer:

mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
    getString(R.string.app_font)));

((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
     getAssets(), getString(R.string.app_font)));

Answer:

You do not have to do anything. Just simply set the typeface to the EditText, and that will automatically change the hint font as well.

Here is the custom typeface and how to set it:

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/gobold_uplow.ttf");

Edittext mEmailEntry = (EditText) findViewById(R.id.entry_email);
mEmailEntry.setTypeface(typeface);

Answer:

You should create CustomTypefaceSpan to have different fonts for Hint and EditText.

Typeface hintTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(hintTypeface);
SpannableString spannableString = new SpannableString("some hint text");
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
editText.setHint(spannableString);


public class CustomTypefaceSpan extends TypefaceSpan
{
    private final Typeface mNewType;

    public CustomTypefaceSpan(Typeface type)
    {
        super("");
        mNewType = type;
    }

    public CustomTypefaceSpan(String family, Typeface type)
    {
        super(family);
        mNewType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds)
    {
        applyCustomTypeFace(ds, mNewType);
    }

    @Override
    public void updateMeasureState(TextPaint paint)
    {
        applyCustomTypeFace(paint, mNewType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf)
    {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null)
        {
            oldStyle = 0;
        }
        else
        {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0)
        {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0)
        {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}

Answer:

editText.setTypeface(Typeface.SERIF);