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); | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Wow, very nice. |
This comment has been minimized.
This comment has been minimized.
Nice and simple. Thanks! |
This comment has been minimized.
This comment has been minimized.
Does it work on all Android versions? |
This comment has been minimized.
This comment has been minimized.
Awesome! |
This comment has been minimized.
This comment has been minimized.
awesome! solved my long time problem with Typeface! |
This comment has been minimized.
This comment has been minimized.
thx! |
This comment has been minimized.
This comment has been minimized.
Doesn't work if font-size is specified. |
This comment has been minimized.
This comment has been minimized.
It's working perfectly fine... (Y).. thanks... |
This comment has been minimized.
This comment has been minimized.
Thank you, It's works fine in Arabic text, all over the app. |
This comment has been minimized.
This comment has been minimized.
Awesome |
This comment has been minimized.
This comment has been minimized.
cool feint ears! |
This comment has been minimized.
This comment has been minimized.
Wow nice one :) |
This comment has been minimized.
This comment has been minimized.
wow..thank you so much.. :) |
This comment has been minimized.
This comment has been minimized.
thanks alot , you saved me |
This comment has been minimized.
This comment has been minimized.
Thanks a lot ,what is the minimum android version your class will support?Is Ice cream Sandwich? |
This comment has been minimized.
This comment has been minimized.
where to put themes.xml file..?in which folder..? |
This comment has been minimized.
This comment has been minimized.
Thank A Lot... Good Work... :) |
This comment has been minimized.
This comment has been minimized.
I wander that is not working over 5.0 |
This comment has been minimized.
This comment has been minimized.
Thank A Lot. |
This comment has been minimized.
This comment has been minimized.
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" > |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
works like a charm! |
This comment has been minimized.
This comment has been minimized.
this is perfect and help me so much.... thank you... thank you .... thank you |
This comment has been minimized.
This comment has been minimized.
does this work on L projects using app compat? coz i can't seem to see the difference :( |
This comment has been minimized.
This comment has been minimized.
How to set for DEFAULT and DEFAULT_BOLD both? |
This comment has been minimized.
This comment has been minimized.
Thank you so much. Awesome. So many thanks to you |
This comment has been minimized.
This comment has been minimized.
Thank you, I like this way of setting a custom font for the entire app ! |
This comment has been minimized.
This comment has been minimized.
How do you make this work for Lollipop and above? |
This comment has been minimized.
This comment has been minimized.
This looks much better than finding all the views and changing their typeface everytime a new Activity is created. Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
It works fine. Is there a way to apply it to the textviews that are bold or italic? |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
thanks, i love u <3 |
This comment has been minimized.
This comment has been minimized.
It works for me... Thanks :) |
This comment has been minimized.
This comment has been minimized.
how can we revert back to default. I am doing something like this: |
This comment has been minimized.
This comment has been minimized.
kudos.. great one |
This comment has been minimized.
This comment has been minimized.
It's working fine for me awesome. |
This comment has been minimized.
This comment has been minimized.
This font not supported for DialogFragment class |
This comment has been minimized.
This comment has been minimized.
I used it with Jameel noori nastaleeq font for Urdu. Loving it! |
This comment has been minimized.
This comment has been minimized.
**great really helpful ** |
This comment has been minimized.
This comment has been minimized.
Wow awesome answer. Thanks a lot |
This comment has been minimized.
This comment has been minimized.
After a long time of tries,..Finally. Its worked, thanks. |
This comment has been minimized.
This comment has been minimized.
How to set type face in style.xml |
This comment has been minimized.
This comment has been minimized.
where & how to use myapp.java ?? |
This comment has been minimized.
This comment has been minimized.
Nice.. All of the font are nicely replaced but Toolbar title cannot change? How could I change it? |
This comment has been minimized.
This comment has been minimized.
Can you please show this example in working HelloWorld Application ? |
This comment has been minimized.
This comment has been minimized.
You are a lifesaver, thank you |
This comment has been minimized.
This comment has been minimized.
To change EditText's password font: CustomTextInput example:
activity_login.xml example:
|
This comment has been minimized.
This comment has been minimized.
What to replace the qustion mark. kindly advice me. I have a font file named myfont.otf public class TypefaceUtil {
} |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
@boopathikumar018: You can avoid that variable and change the definition of overrideFont method and change the function calling appropriately, like |
This comment has been minimized.
This comment has been minimized.
is the text inside button also gets changed with this? |
This comment has been minimized.
This comment has been minimized.
@rustyJ4ck not working in some devices like samsung s4 |
This comment has been minimized.
This comment has been minimized.
How to add punjabi or hindi language in listview |
This comment has been minimized.
This comment has been minimized.
How to override sans-serif-medium (or light)? |
This comment has been minimized.
This comment has been minimized.
Thanks a lot. |
This comment has been minimized.
This comment has been minimized.
thanx man it worked. |
This comment has been minimized.
This comment has been minimized.
there is an step missing "android:name=".MyApp" add this to your Menifiest file under Application. |
This comment has been minimized.
This comment has been minimized.
absolutely brilliant hats off, away some. i don't have word to say. |
This comment has been minimized.
This comment has been minimized.
you are awesome |
This comment has been minimized.
This comment has been minimized.
awesome |
This comment has been minimized.
This comment has been minimized.
don't work in api > 21 |
This comment has been minimized.
This comment has been minimized.
Simply Awesome |
This comment has been minimized.
This comment has been minimized.
NOT WORKING WITH API >21 |
This comment has been minimized.
This comment has been minimized.
very brilliant logic,, your are god,,, you save time of lot of people... Thanks :) |
This comment has been minimized.
This comment has been minimized.
2018 and does not work with API>21 |
This comment has been minimized.
This comment has been minimized.
Better way of doing that is to create custom text/input field with custom fonts like: `public class RegularProximaTextView extends TextView { public RegularProximaTextView(Context context) { |
This comment has been minimized.
This comment has been minimized.
I just used this and still works. thank you :D |
This comment has been minimized.
This comment has been minimized.
Where we have to put MyApp class. |
This comment has been minimized.
This comment has been minimized.
Can you please show this example in working HelloWorld Application ? |
This comment has been minimized.
This comment has been minimized.
oh my god! how it work!!! it still wow word but unfortunately do not work in api>21 |
This comment has been minimized.
This comment has been minimized.
Thaaaaaaaaaaaaaaaaaank youuuu soooooooooooooo muuuch |
This comment has been minimized.
This comment has been minimized.
Wow That's cool |
This comment has been minimized.
This comment has been minimized.
awesome, nice |
This comment has been minimized.
This comment has been minimized.
is there a way to get this to work when android:textStyle="bold" is specified in the layout xml? |
This comment has been minimized.
This comment has been minimized.
I'm sure this was a life saviour back in 2014 but now for API 26+ you can use this much easier solution |
This comment has been minimized.
This comment has been minimized.
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 comment has been minimized.
This comment has been minimized.
This is great, i created a kotlin version here https://gist.github.com/Johnyoat/040ca5224071d01b3f3dfc6cd4d026f7 using this solution |
This comment has been minimized.
This comment has been minimized.
Thank you,its Amazing! |
This comment has been minimized.
This comment has been minimized.
very nice, thank you |
This comment has been minimized.
Awesome