Is it possible to set typeface of hint element of EditText view without changing the typeface of the EditText itself? Thanks.
Answer. The default font is Times New Roman.
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.
Seems that there is no way to control typeface of hint and typeface of text separately.
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);
}
}
Did you check android:typeface
?
This works fine both for the hint and text for the EditText.
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
Pass the result of fromHtml() to setHint();
myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With