11

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

AADProgramming
  • 6,077
  • 11
  • 38
  • 58
Asahi
  • 13,378
  • 12
  • 67
  • 87
  • 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 Answers10

12

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

Asahi
  • 13,378
  • 12
  • 67
  • 87
3

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) {}
        })
Anne
  • 61
  • 2
2

Did you check android:typeface?

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

Prashanth Babu
  • 208
  • 2
  • 5
2

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>"));
    
Kevin
  • 2,605
  • 2
  • 20
  • 15
1

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);
}
}
Anand Savjani
  • 2,484
  • 3
  • 26
  • 39
1

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());

Dmila Ram
  • 1,054
  • 1
  • 13
  • 19
1

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);
    }
}
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
0

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

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

((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
     getAssets(), getString(R.string.app_font)));
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Mohammad
  • 177
  • 2
  • 9
  • 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
  • 1
    you must set Typeface for TextInputLayout and EditText both. not just for EditText – Mohammad Apr 27 '17 at 07:53
-3

editText.setTypeface(Typeface.SERIF);

Ahmed Nader
  • 151
  • 5
  • 19