Android lacks proper support for custom typefaces. Most obvious method of defining typeface for UI elements via XML attributes is missing from default framework views. This library makes it a lot easier to set custom typefaces from java code - one time initialization inside Application subclas and then applying custom typeface to all View's via typeface() static method call.
Sample app (sources available in sample folder):
- Apply custom typefaces to whole view hierarchy via single call.
- Super easy initialization in Application subclass via
TypefaceHelper.init() TypefaceHelper.typeface()applies custom typeface to allTextView's insideActivityorViewGroupTypefaceHelpercan also apply custom typeface toView, or createSpannableStringwith custom font fromStrning/CharSequence- Support for multiple typefaces:
TypefaceHelpercan use defaultTypefaceCollectionpassed viainit()or can be parametrized with otherTypefaceCollection - Support for textStyles:
Typeface.NORMAL,Typeface.BOLD,Typeface.ITALICandTypeface.BOLD_ITALIC - Support for dynamic typeface changes
- Support for custom typefaces in ActionBar title
Via gradle:
compile 'com.norbsoft.typefacehelper:library:0.9.0' Alternative download AAR here
- Put your custom true type fonts (TTF) in
assets/fontsfolder - Pass
TypefaceCollectiontoTypefaceHelper.init()in yourApplicationsubclass:
@Override public void onCreate() { super.onCreate(); // Initialize typeface helper TypefaceCollection typeface = new TypefaceCollection.Builder() .set(Typeface.NORMAL, Typeface.createFromAsset(getAssets(), "fonts/ubuntu/Ubuntu-R.ttf")) .create(); TypefaceHelper.init(typeface); }- Apply custom typefaces to
Activity,ViewGrouporView:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); // Apply custom typefaces! TypefaceHelper.typeface(this); } You can use static import for typeface() method:
import static com.norbsoft.typefacehelper.TypefaceHelper.typeface; and then use it without referencing TypefaceHelper class:
View v = inflater.inflate(R.layout.myview); typeface(v);Pass ActionBar instance along with SpannableString with custom typeface to ActionBarHelper.setTitle() helper:
ActionBarHelper.setTitle( getSupportActionBar(), typeface(this, R.string.lorem_ipsum_title));Each time ConvertView is created inside adapter, it needs to be styled via typeface() call
listView.setAdapter(new BaseAdapter() { String[] items = { "Item 1", "Item 2", "Item 3" }; @Override public int getCount() { return items.length; } @Override public Object getItem(int position) { return items[position]; } @Override public long getItemId(int position) { return items[position].hashCode(); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context) .inflate(android.R.layout.simple_list_item_1, parent, false); // Apply custom typeface! typeface(convertView); } ((TextView) convertView).setText(items[position]); return convertView; } });First, define and initialize TypefaceCollection for each typeface you intend to use in your Application subclass:
public class MyApplication extends Application { private TypefaceCollection mArchRivalTypeface; private TypefaceCollection mUbuntuTypeface; @Override public void onCreate() { super.onCreate(); // Initialize Arch Rival typeface mArchRivalTypeface = new TypefaceCollection.Builder() .set(Typeface.NORMAL, Typeface.createFromAsset(getAssets(), "fonts/arch_rival/SF_Arch_Rival.ttf")) .set(Typeface.BOLD, Typeface.createFromAsset(getAssets(), "fonts/arch_rival/SF_Arch_Rival_Bold.ttf")) .set(Typeface.ITALIC, Typeface.createFromAsset(getAssets(), "fonts/arch_rival/SF_Arch_Rival_Italic.ttf")) .set(Typeface.BOLD_ITALIC, Typeface.createFromAsset(getAssets(), "fonts/arch_rival/SF_Arch_Rival_Bold_Italic.ttf")) .create(); // Initialize Ubuntu typeface mUbuntuTypeface = new TypefaceCollection.Builder() .set(Typeface.NORMAL, Typeface.createFromAsset(getAssets(), "fonts/ubuntu/Ubuntu-R.ttf")) .set(Typeface.BOLD, Typeface.createFromAsset(getAssets(), "fonts/ubuntu/Ubuntu-B.ttf")) .set(Typeface.ITALIC, Typeface.createFromAsset(getAssets(), "fonts/ubuntu/Ubuntu-RI.ttf")) .set(Typeface.BOLD_ITALIC, Typeface.createFromAsset(getAssets(), "fonts/ubuntu/Ubuntu-BI.ttf")) .create(); // Load helper with default custom typeface (single custom typeface) TypefaceHelper.init(mUbuntuTypeface); } /** Getter for Arch Rival typeface */ public TypefaceCollection getArchRivalTypeface() { return mArchRivalTypeface; } /** Getter for Ubuntu typeface */ public TypefaceCollection getUbuntuTypeface() { return mUbuntuTypeface; } }Then, call typeface() and pass custmo TypefaceCollection:
typeface(findViewById(R.id.label_title), ((MyApplication) getApplication()).getArchRivalTypeface()); typeface(findViewById(R.id.label_subtitle), ((MyApplication) getApplication()).getUbuntuTypeface());Typeface changes each time typeface() is called - it can be used from onClick() method:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_font_ubuntu: typeface(findViewById(R.id.label_title), ((MyApplication) getApplication()).getUbuntuTypeface()); break; case R.id.btn_font_arch_rival: typeface(findViewById(R.id.label_title), ((MyApplication) getApplication()).getArchRivalTypeface()); break; } }Android Typeface Helper is licensed under [http://www.apache.org/licenses/LICENSE-2.0](Apache License 2.0.)
