Skip to content

Instantly share code, notes, and snippets.

@artem-zinnatullin
Last active January 15, 2023 13:04
Show Gist options
  • Save artem-zinnatullin/7749076 to your computer and use it in GitHub Desktop.
Save artem-zinnatullin/7749076 to your computer and use it in GitHub Desktop.
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);
}
}
}
@RakeshDextrous
Copy link

Where we have to put MyApp class.

@RakeshDextrous
Copy link

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

Wow That's cool

@gundubandagar
Copy link

awesome, nice

@Francis-Smith
Copy link

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

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

Thank you,its Amazing!

@FirstVoyager
Copy link

very nice, thank you

@MrCalifer
Copy link

Works perfectly

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