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);
}
}
}
@har2008preet
Copy link

har2008preet commented Jul 19, 2017

is the text inside button also gets changed with this?

@arpitjoshi08
Copy link

arpitjoshi08 commented Jul 25, 2017

@rustyJ4ck not working in some devices like samsung s4

@amolaksingh
Copy link

amolaksingh commented Jul 26, 2017

How to add punjabi or hindi language in listview

@momt99
Copy link

momt99 commented Aug 3, 2017

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

@2860166
Copy link

2860166 commented Aug 18, 2017

Thanks a lot.

@jughoor
Copy link

jughoor commented Sep 8, 2017

thanx man it worked.

@jughoor
Copy link

jughoor commented Sep 8, 2017

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

@gundubandagar
Copy link

gundubandagar commented Sep 11, 2017

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

@preethambogadi
Copy link

preethambogadi commented Sep 21, 2017

you are awesome

@shaikhabdulqadir
Copy link

shaikhabdulqadir commented Sep 27, 2017

awesome

@MansoorJafari
Copy link

MansoorJafari commented Nov 6, 2017

don't work in api > 21

@faizrehman1
Copy link

faizrehman1 commented Dec 26, 2017

Simply Awesome

@DPatel02
Copy link

DPatel02 commented Dec 27, 2017

NOT WORKING WITH API >21

@gundubandagar
Copy link

gundubandagar commented Dec 28, 2017

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

@sud007
Copy link

sud007 commented Jan 3, 2018

2018 and does not work with API>21

@AhmadAyyaz1993
Copy link

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
Copy link

meidikawardana commented May 24, 2018

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

@RakeshDextrous
Copy link

RakeshDextrous commented Jun 6, 2018

Where we have to put MyApp class.

@RakeshDextrous
Copy link

RakeshDextrous commented Jun 6, 2018

Can you please show this example in working HelloWorld Application ?

@radeshf
Copy link

radeshf commented Jun 18, 2018

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

@SinaMN75
Copy link

SinaMN75 commented Jul 5, 2018

Thaaaaaaaaaaaaaaaaaank youuuu soooooooooooooo muuuch

@harpreetsi9h
Copy link

harpreetsi9h commented Jul 6, 2018

Wow That's cool

@gundubandagar
Copy link

gundubandagar commented Dec 6, 2018

awesome, nice

@Francis-Smith
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

Abhish-Bhawsar commented Oct 20, 2019

Thank you,its Amazing!

@FirstVoyager
Copy link

FirstVoyager commented Oct 27, 2019

very nice, thank you

@MrCalifer
Copy link

MrCalifer commented Apr 24, 2021

Works perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment