Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
If you need to set one font for all TextViews in android application you can use this solution. It will override ALL TextView's typefaces, includes action bar and other standard components, but EditText's password font won't be overriden.
public class MyApp extends Application {
@Override
public void onCreate() {
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<!-- you should set typeface which you want to override with TypefaceUtil -->
<item name="android:typeface">serif</item>
</style>
</resources>
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.lang.reflect.Field;
public class TypefaceUtil {
/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
@lgvalle

This comment has been minimized.

Copy link

@lgvalle lgvalle commented Jul 21, 2014

Awesome

@Krylez

This comment has been minimized.

Copy link

@Krylez Krylez commented Jul 21, 2014

Wow, very nice.

@micnoy

This comment has been minimized.

Copy link

@micnoy micnoy commented Jul 22, 2014

Nice and simple. Thanks!

@martijndebruijn

This comment has been minimized.

Copy link

@martijndebruijn martijndebruijn commented Jul 29, 2014

Does it work on all Android versions?

@rabin111

This comment has been minimized.

Copy link

@rabin111 rabin111 commented Aug 21, 2014

Awesome!

@ngheungyu

This comment has been minimized.

Copy link

@ngheungyu ngheungyu commented Sep 1, 2014

awesome! solved my long time problem with Typeface!

@rustyJ4ck

This comment has been minimized.

Copy link

@rustyJ4ck rustyJ4ck commented Oct 2, 2014

thx!

@gauravsapiens

This comment has been minimized.

Copy link

@gauravsapiens gauravsapiens commented Oct 10, 2014

Doesn't work if font-size is specified.

@RahulRanjan-TPL

This comment has been minimized.

Copy link

@RahulRanjan-TPL RahulRanjan-TPL commented Oct 13, 2014

It's working perfectly fine... (Y).. thanks...

@shoully

This comment has been minimized.

Copy link

@shoully shoully commented Oct 15, 2014

Thank you, It's works fine in Arabic text, all over the app.

@iflashi

This comment has been minimized.

Copy link

@iflashi iflashi commented Dec 21, 2014

Awesome

@gukzilla

This comment has been minimized.

Copy link

@gukzilla gukzilla commented Dec 24, 2014

cool feint ears!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 21, 2015

Wow nice one :)

@shyamjptpm

This comment has been minimized.

Copy link

@shyamjptpm shyamjptpm commented Mar 3, 2015

wow..thank you so much.. :)

@vaaniiaa

This comment has been minimized.

Copy link

@vaaniiaa vaaniiaa commented Mar 31, 2015

thanks alot , you saved me

@rajeshgoswami2025

This comment has been minimized.

Copy link

@rajeshgoswami2025 rajeshgoswami2025 commented May 4, 2015

Thanks a lot ,what is the minimum android version your class will support?Is Ice cream Sandwich?

@gayupatel

This comment has been minimized.

Copy link

@gayupatel gayupatel commented May 5, 2015

where to put themes.xml file..?in which folder..?

@ashishsharma30

This comment has been minimized.

Copy link

@ashishsharma30 ashishsharma30 commented May 6, 2015

Thank A Lot... Good Work... :)

@wangyeming

This comment has been minimized.

Copy link

@wangyeming wangyeming commented May 27, 2015

I wander that is not working over 5.0

@mgolkardev

This comment has been minimized.

Copy link

@mgolkardev mgolkardev commented Jun 24, 2015

Thank A Lot.

@hrsalehi

This comment has been minimized.

Copy link

@hrsalehi hrsalehi commented Jul 4, 2015

I know for android 5 (api > 21) we should add an values-v21 style but nothing change. can some one help me?

my values-21 -> style.xml

< style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar" >
< / style>
< style name="AppTheme" parent="AppBaseTheme" >
< item name="android:typeface">serif< / item>
< / style>

@hrsalehi

This comment has been minimized.

Copy link

@hrsalehi hrsalehi commented Jul 4, 2015

I asked the question on stackoverflow and found the answer (for api >= 21 the older solution doesn't work): http://stackoverflow.com/questions/31218795/changing-all-typefaces-of-an-android-application/31219701#31219701

The second answer worked for me

@mahyarmblb

This comment has been minimized.

Copy link

@mahyarmblb mahyarmblb commented Jul 9, 2015

works like a charm!

@sdarvish

This comment has been minimized.

Copy link

@sdarvish sdarvish commented Jul 15, 2015

this is perfect and help me so much.... thank you... thank you .... thank you

@rohan90

This comment has been minimized.

Copy link

@rohan90 rohan90 commented Aug 6, 2015

does this work on L projects using app compat? coz i can't seem to see the difference :(

@prankulg

This comment has been minimized.

Copy link

@prankulg prankulg commented Aug 20, 2015

How to set for DEFAULT and DEFAULT_BOLD both?

@thuongleit

This comment has been minimized.

Copy link

@thuongleit thuongleit commented Aug 24, 2015

Thank you so much. Awesome. So many thanks to you

@machadaCosta

This comment has been minimized.

Copy link

@machadaCosta machadaCosta commented Oct 7, 2015

Thank you, I like this way of setting a custom font for the entire app !

@osugo

This comment has been minimized.

Copy link

@osugo osugo commented Oct 23, 2015

How do you make this work for Lollipop and above?

@RaySuhyunLee

This comment has been minimized.

Copy link

@RaySuhyunLee RaySuhyunLee commented Oct 28, 2015

This looks much better than finding all the views and changing their typeface everytime a new Activity is created. Thanks a lot!

@benino

This comment has been minimized.

Copy link

@benino benino commented Nov 14, 2015

I'd love to try this solution. I have source code for an app written by someone else and I need to change the font. I'm new to android development. Where do I add these three files and how do I get the app to actually execute this code?

@nickstamp93

This comment has been minimized.

Copy link

@nickstamp93 nickstamp93 commented Jan 14, 2016

It works fine. Is there a way to apply it to the textviews that are bold or italic?

@solankiherry

This comment has been minimized.

Copy link

@solankiherry solankiherry commented Jan 21, 2016

hey friend, i try this solution but cant override serif font.so font change but display serif font so something wrong at use of myapp file.
any one tell me how to solve it.
and when is use or declare myapp file.
help me....

@rida1148

This comment has been minimized.

Copy link

@rida1148 rida1148 commented Feb 10, 2016

thanks, i love u <3

@Akshi187

This comment has been minimized.

Copy link

@Akshi187 Akshi187 commented Feb 15, 2016

It works for me... Thanks :)

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 15, 2016

how can we revert back to default.

I am doing something like this:
if(configShared.getString("storeId", AppCredentials.DEFAULT_STORE_ID).equals("2")){
final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, newTypeface);
}
else{
final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, null);
}

@phanii

This comment has been minimized.

Copy link

@phanii phanii commented Apr 1, 2016

kudos.. great one

@truetechchandru

This comment has been minimized.

Copy link

@truetechchandru truetechchandru commented Aug 19, 2016

It's working fine for me awesome.

@truetechchandru

This comment has been minimized.

Copy link

@truetechchandru truetechchandru commented Aug 19, 2016

This font not supported for DialogFragment class

@shznaqvi

This comment has been minimized.

Copy link

@shznaqvi shznaqvi commented Sep 22, 2016

I used it with Jameel noori nastaleeq font for Urdu. Loving it!

@rafikmechria

This comment has been minimized.

Copy link

@rafikmechria rafikmechria commented Sep 25, 2016

**

great really helpful

**

@mansi-kulkarni

This comment has been minimized.

Copy link

@mansi-kulkarni mansi-kulkarni commented Nov 17, 2016

Wow awesome answer. Thanks a lot

@TingaZ

This comment has been minimized.

Copy link

@TingaZ TingaZ commented Feb 1, 2017

After a long time of tries,..Finally. Its worked, thanks.

@isrinath

This comment has been minimized.

Copy link

@isrinath isrinath commented Feb 8, 2017

How to set type face in style.xml
e.g: ?????????

@ReenaMandwariya

This comment has been minimized.

Copy link

@ReenaMandwariya ReenaMandwariya commented Feb 24, 2017

where & how to use myapp.java ??

@LuuMaw

This comment has been minimized.

Copy link

@LuuMaw LuuMaw commented May 14, 2017

Nice.. All of the font are nicely replaced but Toolbar title cannot change? How could I change it?

@amaneer94

This comment has been minimized.

Copy link

@amaneer94 amaneer94 commented May 15, 2017

Can you please show this example in working HelloWorld Application ?

@kyndaqtepammy

This comment has been minimized.

Copy link

@kyndaqtepammy kyndaqtepammy commented May 17, 2017

You are a lifesaver, thank you

@juanbrusco

This comment has been minimized.

Copy link

@juanbrusco juanbrusco commented May 31, 2017

To change EditText's password font:
Create CustomTextInput and CustomEditText inside the package called "components" and then use it into layout:

CustomTextInput example:

public class CustomTextInput extends TextInputLayout{
    private Typeface faceLight;

    public CustomTextInput(Context context) {
        super(context);
        setFont(context);
    }

    public CustomTextInput(Context context, AttributeSet set) {
        super(context, set);
        setFont(context);
    }

    public CustomTextInput(Context context, AttributeSet set, int defaultStyle) {
        super(context, set, defaultStyle);
        setFont(context);
    }

    private void setFont(Context context) {
        faceLight = Typeface.createFromAsset(context.getApplicationContext().getAssets(), Constants.faceLightPath);
        setTypeface(faceLight);
    }

}

activity_login.xml example:


        <components.CustomTextInput
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="30dp"
            >

            <components.CustomEditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:inputType="textPassword" />
        </components.CustomTextInput>
@boopathikumar018

This comment has been minimized.

Copy link

@boopathikumar018 boopathikumar018 commented Jun 25, 2017

What to replace the qustion mark. kindly advice me. I have a font file named myfont.otf

public class TypefaceUtil {

public static void overrideFont(Context context, String momospace, String ?? ) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), "myfont.otf");

        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(momospace);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
    } catch (Exception e) {

    }
}

}

@amruta-pani

This comment has been minimized.

Copy link

@amruta-pani amruta-pani commented Jul 18, 2017

Thanks and it worked. I didnt know that the default typefaces that could be overwritten are predefined but I could realize it soon. Thanks again

@amruta-pani

This comment has been minimized.

Copy link

@amruta-pani amruta-pani commented Jul 18, 2017

@boopathikumar018: You can avoid that variable and change the definition of overrideFont method and change the function calling appropriately, like overrideFont(getApplicationContext(), "SERIF") in the application subclass.
Or you could follow the example as mentioned above and replace the ?? with customFontFileNameInAssets and replace myfont.otf in the try block with the customFontFileNameInAssets.

@har2008preet

This comment has been minimized.

Copy link

@har2008preet har2008preet commented Jul 19, 2017

is the text inside button also gets changed with this?

@arpitjoshi08

This comment has been minimized.

Copy link

@arpitjoshi08 arpitjoshi08 commented Jul 25, 2017

@rustyJ4ck not working in some devices like samsung s4

@amolaksingh

This comment has been minimized.

Copy link

@amolaksingh amolaksingh commented Jul 26, 2017

How to add punjabi or hindi language in listview

@momt99

This comment has been minimized.

Copy link

@momt99 momt99 commented Aug 3, 2017

How to override sans-serif-medium (or light)?

@2860166

This comment has been minimized.

Copy link

@2860166 2860166 commented Aug 18, 2017

Thanks a lot.

@jughoor

This comment has been minimized.

Copy link

@jughoor jughoor commented Sep 8, 2017

thanx man it worked.

@jughoor

This comment has been minimized.

Copy link

@jughoor jughoor commented Sep 8, 2017

there is an step missing "android:name=".MyApp" add this to your Menifiest file under Application.

@gundubandagar

This comment has been minimized.

Copy link

@gundubandagar gundubandagar commented Sep 11, 2017

absolutely brilliant hats off, away some. i don't have word to say.

@preethambogadi

This comment has been minimized.

Copy link

@preethambogadi preethambogadi commented Sep 21, 2017

you are awesome

@shaikhabdulqadir

This comment has been minimized.

Copy link

@shaikhabdulqadir shaikhabdulqadir commented Sep 27, 2017

awesome

@MansoorJafari

This comment has been minimized.

Copy link

@MansoorJafari MansoorJafari commented Nov 6, 2017

don't work in api > 21

@faizrehman1

This comment has been minimized.

Copy link

@faizrehman1 faizrehman1 commented Dec 26, 2017

Simply Awesome

@DPatel02

This comment has been minimized.

Copy link

@DPatel02 DPatel02 commented Dec 27, 2017

NOT WORKING WITH API >21

@gundubandagar

This comment has been minimized.

Copy link

@gundubandagar gundubandagar commented Dec 28, 2017

very brilliant logic,, your are god,,, you save time of lot of people... Thanks :)

@sud007

This comment has been minimized.

Copy link

@sud007 sud007 commented Jan 3, 2018

2018 and does not work with API>21

@AhmadAyyaz1993

This comment has been minimized.

Copy link

@AhmadAyyaz1993 AhmadAyyaz1993 commented Apr 2, 2018

Better way of doing that is to create custom text/input field with custom fonts like:

`public class RegularProximaTextView extends TextView {
public static Typeface FONT_NAME;

public RegularProximaTextView(Context context) {
super(context);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/Mark Simonson - Proxima Nova Regular.otf");
this.setTypeface(FONT_NAME);
}
public RegularProximaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/Mark Simonson - Proxima Nova Regular.otf");
this.setTypeface(FONT_NAME);
}
public RegularProximaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/Mark Simonson - Proxima Nova Regular.otf");
this.setTypeface(FONT_NAME);
}`

@meidikawardana

This comment has been minimized.

Copy link

@meidikawardana meidikawardana commented May 24, 2018

I just used this and still works. thank you :D

@RakeshDextrous

This comment has been minimized.

Copy link

@RakeshDextrous RakeshDextrous commented Jun 6, 2018

Where we have to put MyApp class.

@RakeshDextrous

This comment has been minimized.

Copy link

@RakeshDextrous RakeshDextrous commented Jun 6, 2018

Can you please show this example in working HelloWorld Application ?

@radeshf

This comment has been minimized.

Copy link

@radeshf radeshf commented Jun 18, 2018

oh my god! how it work!!! it still wow word but unfortunately do not work in api>21

@SinaMN75

This comment has been minimized.

Copy link

@SinaMN75 SinaMN75 commented Jul 5, 2018

Thaaaaaaaaaaaaaaaaaank youuuu soooooooooooooo muuuch

@harpreetsi9h

This comment has been minimized.

Copy link

@harpreetsi9h harpreetsi9h commented Jul 6, 2018

Wow That's cool

@gundubandagar

This comment has been minimized.

Copy link

@gundubandagar gundubandagar commented Dec 6, 2018

awesome, nice

@Francis-Smith

This comment has been minimized.

Copy link

@Francis-Smith Francis-Smith commented Dec 13, 2018

is there a way to get this to work when android:textStyle="bold" is specified in the layout xml?

@ayoubm

This comment has been minimized.

Copy link

@ayoubm ayoubm commented Feb 14, 2019

I'm sure this was a life saviour back in 2014 but now for API 26+ you can use this much easier solution 👍
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

@JPsurj1a01

This comment has been minimized.

Copy link

@JPsurj1a01 JPsurj1a01 commented Apr 18, 2019

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

Follow the steps mentioned in the link above.

Just need to add font_family="@fonts/font_type" for changing the font style for any textview or button edittext etc.

@Johnyoat

This comment has been minimized.

Copy link

@Johnyoat Johnyoat commented Jun 13, 2019

This is great, i created a kotlin version here https://gist.github.com/Johnyoat/040ca5224071d01b3f3dfc6cd4d026f7 using this solution

@Abhish-Bhawsar

This comment has been minimized.

Copy link

@Abhish-Bhawsar Abhish-Bhawsar commented Oct 20, 2019

Thank you,its Amazing!

@FirstVoyager

This comment has been minimized.

Copy link

@FirstVoyager FirstVoyager commented Oct 27, 2019

very nice, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.