31

Is there any way to change the font for a EditText in Android? I want it to match the font's I set for all my textViews.

William L.
  • 3,846
  • 9
  • 53
  • 72
  • http://stackoverflow.com/questions/10766716/set-font-for-all-textviews-in-activity/10766791#10766791 – Shankar Agarwal May 26 '12 at 14:10
  • Take a look at [this answer](http://stackoverflow.com/a/41275113/4729523)! It allows you use multiple fonts and using XML. – Rafa0809 Dec 22 '16 at 12:03
  • You can now specify custom fonts right from the XML in android studio 3.0 https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html – Seven Oct 25 '17 at 20:57

8 Answers8

42
 editText.setTypeface(Typeface.SERIF); 

As like as for TextView.

 <TextView
  ...
     android:typeface="serif"
  ... />

Edit: Above is the XML

Roshan
  • 110
  • 2
  • 10
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
27

Solution1:: Just call these method by passing parent view as argument.

private void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
         }
        } else if (v instanceof EditText) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
        }
    } catch (Exception e) {
 }
}

Solution2:: you can subclass the TextView class with your custom font and use it instead of textview.

public class MyEditView extends EditText{

    public MyEditView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyEditView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
            setTypeface(tf);
        }
    }

 }
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
11

Create a fonts folder on assets folder and put your .ttf font file, then into onCreate() function write:

EditText editText =(EditText)findViewById(R.id.insert_yors_edit_text_layout);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); 
editText.setTypeface(type);
Sara
  • 1,844
  • 22
  • 18
9

You can create font folder under res directory in last version Android studio 3 .Then copy and past it you'r font to font folder. Now just add fontFamily to EditText tag xml.

like below.

        <EditText
            android:id="@+id/subject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/headerShare"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/edittext_bg"
            android:fontFamily="@font/ritaric"
            android:gravity="start"
            android:hint="Subject"
            android:inputType="text"
            android:maxHeight="50dp"
            android:padding="16dp"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:textSize="@dimen/colors_textview_size" />

We use android:fontFamily="@font/ritaric" for apply font on EditText.

sirvan alie
  • 411
  • 7
  • 12
1
void setFont(Context context, ViewGroup vg)
        {
          final String FONT_NAME = "lato_bold.ttf";
            for (int i = 0; i < vg.getChildCount(); i++)
                {
                    View v = vg.getChildAt(i);
                    if (v instanceof ViewGroup)
                        setFont(context, (ViewGroup) v);
                    else if (v instanceof TextView)
                        {
                            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof EditText)
                        {
                            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof Button)
                        {
                            ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof CheckBox)
                        {
                            ((CheckBox) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof RadioButton)
                        {
                            ((RadioButton) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                }
        }

In your Activity or Fragment , get the main layout

Inflater inflater = (Inflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//For Activity
//Inflater inflater = (Inflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.main_fragment, container, false);
//For Activity
//View v = inflater.inflate(R.layout.signup_fragment, null, false);
if (v instanceof ViewGroup)
    {
      setFont(getActivity(), (ViewGroup) v);
      //For Activity
     //setFont(this, (ViewGroup) v);
    }
Ishtiaq
  • 1,206
  • 9
  • 18
0

First Method

use this code in your java class

    EditText et_brand=(EditText)findViewById(R.id.et_brand);
    et_brand.setTypeface(Typeface.createFromAsset(getAssets(),"Aspergit Bold.ttf")); 
    //Aspergit Bold.ttf is Font style name of text

Second Method

  1. Create a new java class and it as u want

    public class CustomTextView extends TextView {
    
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) 
    {
    super(context, attrs, defStyle);
    init();
    }
    
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    init();
    }
    
    public CustomTextView(Context context) {
    super(context);
    init();
    }
    
    private void init() {
       Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
    "Aspergit Bold.ttf");
    setTypeface(tf);
    }
    }
    
  2. Use it in your Xml file

                  <YourPackageNAme.CustomEditText
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@null"/>
    
Sunil
  • 3,785
  • 1
  • 32
  • 43
0

Add this code in your java file Which type you want you can add like NORMAL, ITALIC, BOLD, BOLD_ITALIC.

edittext.setTypeface(null, Typeface.ITALIC);

0

lil late but, Simple way

 mEditTextMobileNumber.setTextSize(16.0f);

if you want to change font while typing :

   // Change text size when started entering number
    mEditTextMobileNumber.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s == null || String.valueOf(s).equalsIgnoreCase("")) {
                mEditTextMobileNumber.setTextSize(12.0f);
            } else {
                mEditTextMobileNumber.setTextSize(20.0f);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Makarand
  • 983
  • 9
  • 27