I have defined a font for a EditText and now the EditText hint also shows that font, but i need to use a different font for EditText hint, is there a way to achieve this?
Asked
Active
Viewed 5,087 times
4
-
1You can change the font of EditText but you cannot change the typeface of hint. – Geeky Singh Jan 15 '16 at 06:12
-
you can do one thing change textsize dynamically. on textchange listener – Anjali Tripathi Jan 15 '16 at 06:16
-
@playmaker420 didn't see that post before, anyway thanks – tharinduNA Jan 15 '16 at 06:17
-
This is not duplicate question. voting for reopen it. – ρяσѕρєя K Jan 15 '16 at 06:18
-
AFAIK typeface works for all the text, one cannot separate for hints – Murtaza Khursheed Hussain Jan 15 '16 at 06:21
-
@MikeM. yes exactly that is what I am talking about, what is your solution? – tharinduNA Jan 15 '16 at 09:35
2 Answers
1
Android EditText hint uses the same font that the EditText has
EditText uses textview_hint
layout to show ErrorPopup
popup message.so try as to set different typeface for Hint as:
LayoutInflater inflater = LayoutInflater.from(<Pass context here>);
TextView hintTextView = (TextView) inflater.inflate(
com.android.internal.R.layout.textview_hint, null);
hintTextView.setTypeface(< Typeface Object>);
EDIT:
Because EdiText
internally show Hint popup using ErrorPopup
.
See following link for more help:

ρяσѕρєя K
- 132,198
- 53
- 198
- 213
-
-
@MikeM.: EditText uses internally `ErrorPopup ` to show hint message. – ρяσѕρєя K Jan 15 '16 at 06:27
-
-
Naw, I just thought the OP was talking about the faded background hint, not a pop-up; [like here](http://i.stack.imgur.com/baIMB.png). – Mike M. Jan 15 '16 at 06:40
0
No need to use reflection. You can achieve it by creating your own TypefaceSpan
.
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);
}
}
Use it as:
// set font to EditText itself
Typeface editTextTypeface = Typeface.createFromAsset(getAssets(), "font1.ttf");
EditText editText = (EditText) findViewById(R.id.normalEditText);
editText.setTypeface(editTextTypeface);
// set font to Hint
Typeface hintTypeface = Typeface.createFromAsset(getAssets(), "font2.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);

Ayaz Alifov
- 8,334
- 4
- 61
- 56