I want to type phone number in Persian in android EditText . I set Farsi font to edit text but it didn't work . also i searched a lot but all of them discussed about set Farsi font to static text in Textview not typing in EditText . how can i achieve this ? Thanks
Asked
Active
Viewed 1,155 times
1 Answers
2
Try this:
public class CustomFontEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CustomFontEditText(Context context) {
super(context);
this.context=context;
init();
}
public CustomFontEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
this.attrs=attrs;
init();
}
public CustomFontEditText(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(), "fonts/myfont.ttf");
this.setTypeface(font);
}
@Override
public void setTypeface(Typeface tf, int style) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/farsi.ttf");
super.setTypeface(tf, style);
}
@Override
public void setTypeface(Typeface tf) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/farsi.ttf");
super.setTypeface(tf);
}
NOTE:
Replace farsi.ttf
with your font name and use CustomFontEditText
instead of default EditText
in your xml

mrid
- 5,782
- 5
- 28
- 71
-
If your project doesn't have an asset folder, you must create one: new -> folder -> Assets Folder – AlimItTech Mar 17 '22 at 08:15