-
-
Save artem-zinnatullin/7749076 to your computer and use it in GitHub Desktop.
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); | |
} | |
} | |
} |
@rustyJ4ck not working in some devices like samsung s4
How to add punjabi or hindi language in listview
How to override sans-serif-medium (or light)?
Thanks a lot.
thanx man it worked.
there is an step missing "android:name=".MyApp" add this to your Menifiest file under Application.
absolutely brilliant hats off, away some. i don't have word to say.
you are awesome
awesome
don't work in api > 21
Simply Awesome
NOT WORKING WITH API >21
very brilliant logic,, your are god,,, you save time of lot of people... Thanks :)
2018 and does not work with API>21
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);
}`
I just used this and still works. thank you :D
Where we have to put MyApp class.
Can you please show this example in working HelloWorld Application ?
oh my god! how it work!!! it still wow word but unfortunately do not work in api>21
Thaaaaaaaaaaaaaaaaaank youuuu soooooooooooooo muuuch
Wow That's cool
awesome, nice
is there a way to get this to work when android:textStyle="bold" is specified in the layout xml?
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
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.
This is great, i created a kotlin version here https://gist.github.com/Johnyoat/040ca5224071d01b3f3dfc6cd4d026f7 using this solution
Thank you,its Amazing!
very nice, thank you
Works perfectly
is the text inside button also gets changed with this?