Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting typeface of hint in TextEdit

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

like image 705
Asahi Avatar asked Jan 17 '11 19:01

Asahi


People also ask

What font is TextEdit?

Answer. The default font is Times New Roman.

What is android inputType?

The android:inputType attribute allows you to specify various behaviors for the input method. Most importantly, if your text field is intended for basic text input (such as for a text message), you should enable auto spelling correction with the "textAutoCorrect" value.


4 Answers

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

like image 76
Asahi Avatar answered Oct 19 '22 02:10

Asahi


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);
    }
}
like image 28
Ayaz Alifov Avatar answered Oct 19 '22 00:10

Ayaz Alifov


Did you check android:typeface?

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

like image 23
Prashanth Babu Avatar answered Oct 19 '22 02:10

Prashanth Babu


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>"));
    
like image 41
Kevin Avatar answered Oct 19 '22 02:10

Kevin