Cannot reset or replace default font in the Android application -
i set default font on application, , don't think fits on ui, wanted reset it. don't know how reset it.
here codes, , solutions problem appreciated.
(1) first created fontoverride class replace default font new one.
public class fontsoverride { public static void setdefaultfont(context context, string statictypefacefieldname, string fontassetname) { final typeface regular = typeface.createfromasset(context.getassets(), fontassetname); replacefont(statictypefacefieldname, regular); } protected static void replacefont(string statictypefacefieldname, final typeface newtypeface) { try { final field staticfield = typeface.class.getdeclaredfield(statictypefacefieldname); staticfield.setaccessible(true); staticfield.set(null, newtypeface); } catch(nosuchfieldexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } } }
(2) , created class named appconfig controls overall configuration of application.
public class appconfig extends android.app.application { public static string url_login = "http://marshall.hostzi.com/gruppo/"; public static string url_register = "http://marshall.hostzi.com/gruppo/"; @override public void oncreate() { super.oncreate(); fontsoverride.setdefaultfont(this, "monospace", "fonts/galderglynn.ttf"); } }
(3) , here's style.xml file. there 1 item named monospace monospace replaced ganderglynn.ttf programatically.
<resources> <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <item name="android:typeface">monospace</item> </style>
i tried switching galderglynn.ttf file one, wouldn't work, , fonts still in galderglynn font.
edit here's part of androidmanifest.xml file.
<application android:name="com.marshall.gruppo.app.appcontroller" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" >
i think need add font in yourapp/src/main/assets/fonts/ this:(sorry, can't post image now)
and next steps, change fontpath in config
public class appconfig extends android.app.application { public static string url_login = "http://marshall.hostzi.com/gruppo/"; public static string url_register = "http://marshall.hostzi.com/gruppo/"; @override public void oncreate() { super.oncreate(); fontsoverride.setdefaultfont(this, "monospace", "fonts/helveticaneue.ttf"); } }
finally, recommend awesome libs change default font calligraphy: https://github.com/chrisjenx/calligraphy
Comments
Post a Comment