Is it possible to set a custom font in an Android application?
I tried what is posted here, but I don't know where my extends Application
class is...
Any help?
EDIT:
I tried the following:
Add a new class that extends from Application
Call this new class from my AndroidManifest.xml
.
I went to my style and added it.
MyApp.java:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf");
// This FontsOverride comes from the example I posted above
}
}
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApp"
android:theme="@style/AppTheme">
....
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">default</item>
</style>
But my font is still not changning... any idea?
Then the MyApp
class is called. But no effect on my fonts...
EDIT2: I realized that my buttons apply the custom font after I set a custom style for my buttons. Here is my custom button style:
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
And here is how it looks now:
So: my button is applying the style, but not the TextView
. Any idea on why my custom font is not being applied for all items in application?
Write a class
public class MyApp extends Application{
// Put the onCreate code as you obtained from the post link you reffered
}
now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp
<application
android:name=".MyApp"
...
>
...
</application>
So whenever the App is opened , onCreate method of MyApp class would be invoked , and the font would be set.
Update Put font file under assets/fonts/your_font_file.ttf
Put this line under onCreate method of your application class(MyApp)
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/your_font_file.ttf");
Source File for TypefaceUtil
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) {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("serif", customFontTypeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
try {
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
}
Now update your style.xml file
put the below line your style which is included for your activity in manifest file
<item name="android:typeface">serif</item>
Hope this helps