Is it possible to change the font for the hint displayed in the EditText
field? I want to set the font in the xml itself.
use this code:
edittext.setAccentTypeface(typeface);
You can change it with a SpannableString and a Custom TypefaceSpan.
First, create a Custom TypefaceSpan class:
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);
}
}
Then just set the TypefaceSpan to a SpannableString:
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(typeface);
SpannableString spannableString = new SpannableString(hintText);
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
And finally just set the hint of your EditText:
mEditText.setHint(spannableString);
There is a very simple way to do it. I just did in my App and it worked. The Key is set Facetype of your TextInputLayout too along with 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)));
In Kotlin:
nomeEditText.setText("New Text") //changes the text
nomeEditText.isEnabled = false //disables editable
nomeEditText.setBackgroundColor(Color.parseColor("#ffffff")) //changes background
nomeEditText.setTextColor(Color.parseColor("#737373")) //changes color text
nomeEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP,28f) //changes text size
nomeEditText.typeface = Typeface.create("@font/roboto", Typeface.BOLD) //changes font familly
nomeEditText.gravity = Gravity.CENTER_HORIZONTAL //centralize
I can change the hint font by use this library .
After compiling the library you must create a class of applications and the class definition following command:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
After each activity you want Override it with the following command:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
Take the approach of using a text input layout and placing a text input edit text inside
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout_Til">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editText"/>
</com.google.android.material.textfield.TextInputLayout>
On the Fragment/Activity , Set the font you want for the hint onto the TextInputLayout(text_input_layout_Til) Then set the font on the edit text(editText) as well / If you do this , The text the user will input and the hint will have different fonts