Sunday, 15 January 2012

Not Getting External Font in Android -



Not Getting External Font in Android -

i not getting external font, creating new class, defined external font.

fontstyle.java

public class fontstyle extends activity{ public final static string roboto_regular = "fonts/roboto_regular.ttf"; public typeface font_roboto_regular = typeface.createfromasset(getassets(), roboto_regular); }

and mainactivity.java

public class mainactivity extends activity { fontstyle font_style; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); fontstyling(); } private void fontstyling() { textview test= (textview) findviewbyid(r.id.tv_test); test.settypeface(font_style.font_roboto_regular ); }

i getting error or logcat:

java.lang.runtimeexception: unable start activity componentinfo{com.test/com.test.mainactivity}: java.lang.nullpointerexception

please guy right me: in advance.

first need pass activity context in fontstyle access getassets method. if fontstyle not activity no need extends activity it. alter fontstyle class as:

public class fontstyle { context context; public final static string roboto_regular = "fonts/roboto_regular.ttf"; public fontstyle(context context){ this.context=context; } public typeface getttypeface(){ typeface font_roboto_regular = typeface.createfromasset(context.getassets(),roboto_regular); homecoming font_roboto_regular; } }

now alter activity code set custom font textview :

public class mainactivity extends activity { fontstyle font_style; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); font_style=new fontstyle(mainactivity.this); fontstyling(); } private void fontstyling() { textview test= (textview) findviewbyid(r.id.tv_test); test.settypeface(font_style.getttypeface()); }

android fonts

No comments:

Post a Comment