Is it possible to set typeface of hint element of EditText view without changing the typeface of the EditText itself? Thanks.
-
Possible duplicate of [change font for editText hint](http://stackoverflow.com/questions/13251990/change-font-for-edittext-hint) – francisco_ssb May 19 '16 at 13:51
10 Answers
Seems that there is no way to control typeface of hint and typeface of text separately.

- 13,378
- 12
- 67
- 87
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) {}
})

- 61
- 2
Did you check android:typeface
?
This works fine both for the hint and text for the EditText.

- 208
- 2
- 5
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
- Wrap your hint text in HTML style tags
- Pass the marked-up String to Html.fromHtml(String htmlString)
Pass the result of fromHtml() to setHint();
myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));

- 2,605
- 2
- 20
- 15
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);
}
}

- 2,484
- 3
- 26
- 39
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:
- remove android:inputType="textPassword" from xml
- apply typeface using this library
set passwordtransformation in code:
password.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); password.setTransformationMethod(PasswordTransformationMethod.getInstance());

- 1,054
- 1
- 13
- 19
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);
}
}

- 8,334
- 4
- 61
- 56
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
mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
getString(R.string.app_font)));
((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
getAssets(), getString(R.string.app_font)));
-
Can you add some explanation to this code? Also, how is this different than [Shahid's answer](http://stackoverflow.com/a/38913148/4660897)? – Tot Zam Apr 26 '17 at 18:44
-
1you must set Typeface for TextInputLayout and EditText both. not just for EditText – Mohammad Apr 27 '17 at 07:53
editText.setTypeface(Typeface.SERIF);

- 151
- 5
- 19
-
editText.setTypeface(Typeface.SERIF); use it to change the font type of edittext – Ahmed Nader Dec 26 '15 at 12:43