android - Setting Custom Font for TextView causing the onCreate to take longer to build -


i implementing roboto font within project. fragments, there lot of textview's. creating custom view exends textview implement custom fonts. there better way load fonts without increasing oncreate times?

extends textview

public class textviewfont extends textview {      public textviewfont(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);         init(attrs);     }      public textviewfont(context context, attributeset attrs) {         super(context, attrs);         init(attrs);      }      public textviewfont(context context) {         super(context);         init(null);     }      private void init(attributeset attrs) {         if (attrs != null) {             typedarray = getcontext().obtainstyledattributes(attrs, r.styleable.textviewfont);             string fontname = a.getstring(r.styleable.textviewfont_fontname);             if (fontname != null) {                 typeface mytypeface = typeface.createfromasset(getcontext().getassets(), "fonts/" + fontname);                 settypeface(mytypeface);             }             a.recycle();         }     } } 

xml

<com.eugene.fithealthmaingit.custom.textviewfont             android:layout_width="0dp"             android:layout_height="wrap_content"             android:layout_marginleft="16dp"             android:layout_weight="1"             android:text="@string/dinner"             android:textcolor="@color/text_color"             android:textsize="16sp"             app:fontname="roboto-regular.ttf"/> 

example of how many textview's

enter image description here

typeface.createfromassets time taking process. should declare typeface static varaiable in class , use in constructor.

but here loading fonts in every textview's constructor.

if having multiple fonts, have tytypeface static , use appropriately.

update code:

public class textviewfont extends textview {          public static typeface typeface1 = typeface.createfromasset(getcontext().getassets(), "fonts/fontname1");         public static typeface typeface2 = typeface.createfromasset(getcontext().getassets(), "fonts/fontname2");         public static typeface typeface3 = typeface.createfromasset(getcontext().getassets(), "fonts/fontname3");         public static typeface typeface4 = typeface.createfromasset(getcontext().getassets(), "fonts/fontname4");         public static typeface typeface5 = typeface.createfromasset(getcontext().getassets(), "fonts/fontname5");          public textviewfont(context context, attributeset attrs, int defstyle) {             super(context, attrs, defstyle);             init(attrs);         }          public textviewfont(context context, attributeset attrs) {             super(context, attrs);             init(attrs);          }          public textviewfont(context context) {             super(context);             init(null);         }          private void init(attributeset attrs) {             if (attrs != null) {                 typedarray = getcontext().obtainstyledattributes(attrs, r.styleable.textviewfont);                 string fontname = a.getstring(r.styleable.textviewfont_fontname);                 if (fontname != null) {                     settypeface(gettypeface(fontname));                 }                 a.recycle();             }         }           public typeface gettypeface(string fontname){             if(fontname.equals("fontname1")){                 return typeface1;             }else if(fontname.equals("fontname2")){                 return typeface2;             }else if(fontname.equals("fontname3")){                 return typeface3;             }else if(fontname.equals("fontname4")){                 return typeface4;             }else if(fontname.equals("fontname5")){                 return typeface5;             }         }     }  } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -