Typeface helper for Android

Overview

Android Typeface Helper

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.

alt text

Sample app (sources available in sample folder):

Get it on Google Play

Features

  • 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 all TextView's inside Activity or ViewGroup
  • TypefaceHelper can also apply custom typeface to View, or create SpannableString with custom font from Strning/CharSequence
  • Support for multiple typefaces: TypefaceHelper can use default TypefaceCollection passed via init() or can be parametrized with other TypefaceCollection
  • Support for textStyles: Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC and Typeface.BOLD_ITALIC
  • Support for dynamic typeface changes
  • Support for custom typefaces in ActionBar title

Installation

Via gradle:

compile 'com.norbsoft.typefacehelper:library:0.9.0'

Alternative download AAR here

Usage

  • Put your custom true type fonts (TTF) in assets/fonts folder
  • Pass TypefaceCollection to TypefaceHelper.init() in your Application subclass:
@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, ViewGroup or View:
@Override protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_layout);
	// Apply custom typefaces!
	TypefaceHelper.typeface(this);
}   

Advanced usage

Static import for typeface() method

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);

Change font in ActionBar title

Pass ActionBar instance along with SpannableString with custom typeface to ActionBarHelper.setTitle() helper:

ActionBarHelper.setTitle(
	getSupportActionBar(), 
	typeface(this, R.string.lorem_ipsum_title));

Apply in AdapterView (ListView, Spinner)

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;
    }
});

Use multiple typefaces and styles

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());

Change typefaces dynamically

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;
	}
}

License

Android Typeface Helper is licensed under [http://www.apache.org/licenses/LICENSE-2.0](Apache License 2.0.)

You might also like...
An Android helper class to manage database creation and version management using an application's raw asset files

THIS PROJECT IS NO LONGER MAINTAINED Android SQLiteAssetHelper An Android helper class to manage database creation and version management using an app

Android Bluetooth Helper Library, Bluetooth Device Finder

Bluetooth Helper Allows you to access the Bluetooth of your mobile device, manage turn-on - turn off, and discover bluetooth devices around you. Getti

TensorFlow Lite Helper for Android to help getting started with TesnorFlow.

TensorFlow Lite Helper for Android This library helps with getting started with TensorFlow Lite on Android. Inspired by TensorFlow Lite Android image

[Android Library] A SharedPreferences helper library to save and fetch the values easily.

Preference Helper A SharedPreferences helper library to save and fetch the values easily. Featured in Use in your project Add this to your module's bu

An Android helper class to manage database creation and version management using an application's raw asset files

THIS PROJECT IS NO LONGER MAINTAINED Android SQLiteAssetHelper An Android helper class to manage database creation and version management using an app

Helper to upload Gradle Android Artifacts, Gradle Java Artifacts and Gradle Kotlin Artifacts to Maven repositories (JCenter, Maven Central, Corporate staging/snapshot servers and local Maven repositories).

GradleMavenPush Helper to upload Gradle Android Artifacts, Gradle Java Artifacts and Gradle Kotlin Artifacts to Maven repositories (JCenter, Maven Cen

A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.

##fb-android-dagger A set of helper classes for using dagger with Android components such as Applications, Activities, Fragments, BroadcastReceivers,

Android ripple animation helper, easy to create Circular Reveal. | Android水波动画帮助类,轻松实现View show/hide/startActivity()特效。(0.4.6)
Android ripple animation helper, easy to create Circular Reveal. | Android水波动画帮助类,轻松实现View show/hide/startActivity()特效。(0.4.6)

CircularAnim English | 中文 首先来看一个UI动效图。 效果图是是Dribbble上看到的,原作品在此。 我所实现的效果如下: Watch on YouTube Compile 最新可用版本 So,你可以如下compile该项目,也可以直接把这个类 CircularAnim 拷

Android SharedPreferences Helper
Android SharedPreferences Helper

PocketDB Android SharedPreferences Helper This is SharedPreferences Helper like a database noSql. Support AES encryption Latest Version Download depen

This library is a helper for the Android Compose Navigation routes

ComposableRoutes This library is a helper for the Android Compose Navigation routes: generates routes for the annotated screens provides helpers to re

Helper class to make any view rotatable
Helper class to make any view rotatable

Rotatable This is a helper class actually, it simplifies having a view as rotatable by setting touch events and handling a lot of boilerplate works! S

A helper library to help using Room with existing pre-populated database [].

Room now supports using a pre-packaged database out of the box, since version 2.2.0 https://developer.android.com/jetpack/androidx/releases/room#2.2.0

A helper library to ease the most repetitive codes with simple reusable attributes.

ak-universal-android-helper A helper library to ease the most repetitive codes with simple reusable attributes. AKUAH can help you with many repetitiv

AppCode helper for Kotlin/Native and Xcode

Kotlin Xcode compatibility Gradle plugin The plugin is used by AppCode to set up Kotlin/Native project along with Xcode Sources A multi-build sample w

Generate helper methods for compose navigation using KSP

Compose NavGen Generate helper methods for compose navigation using KSP. 🚧 You can try it now, but it's still under development. 🚧 TODO Support defa

Helper functions for making Approvals-Java more Kotlin friendly

Approvals-Kt Helper functions for making Approvals-Java more Kotlin-friendly Usage Verify using Approvals-Kt import com.github.greghynds.approvals.Kot

A java flight recording helper

FlightMemory A java flight recording helper Helper class for production profiling using java flight recorder (JFR) FlightMemory.recordingAsZip(Duratio

Item Helper For Kotlin

Item Helper For Kotlin

Helper tool for calculating scales for Teenage Engineering Pocket Operator PO-33/133 & PO-35/137 series.
Helper tool for calculating scales for Teenage Engineering Pocket Operator PO-33/133 & PO-35/137 series.

I heard you like Pocket Operators, so I made Pocket Scale Calculator for your Pocket Operator. This console app is a helper tool for calculating scale

Owner
Norbsoft Sp. z o.o.
Norbsoft Sp. z o.o.
Custom font library for android | Library to change/add font of Entire Android Application at once without wasting your time - TextViews, EditText, Buttons, Views etc.,

AppFontChanger In a Single shot change font of Entire Android Application - TextViews, EditText, Buttons, Views etc., Kindly use the following links t

Prabhakar Thota 48 Aug 10, 2022
Custom fonts in Android the easy way...

This version of Calligraphy has reached its end-of-life and is no longer maintained. Please migrate to Calligraphy 3! Calligraphy Custom fonts in Andr

Christopher Jenkins 8.6k Jan 3, 2023
A lightweight Android library for use iconic fonts.

Print A lightweight Android library for use iconic fonts. Download Gradle: compile 'com.github.johnkil.print:print:1.3.1' Maven: <dependency> <gro

Evgeny Shishkin 202 Dec 27, 2022
A lightweight Android library for use iconic fonts.

Print A lightweight Android library for use iconic fonts. Download Gradle: compile 'com.github.johnkil.print:print:1.3.1' Maven: <dependency> <gro

Evgeny Shishkin 202 Dec 27, 2022
An android library to display FontAwesome Icons in any View or a MenuItem

DroidAwesome A library to display FontAwesome Icons in any View or a MenuItem Views Supported: TextView AutoComplete TextView EditText Switch CheckBox

Livin 38 Aug 25, 2022
Android Library to use custom fonts with ease.

FontometricsLibrary A Simple Android Library to use Custom Fonts with Ease. Use Customs Fonts in your Android project without adding any .ttf/.otf in

Ishmeet Singh 188 Nov 15, 2022
Fontize is an Android library that enables multi-font selection functionality to diversify your app.

Fontize Android Library Built with ❤︎ by Gourav Khunger Fontize is an Android library, written in kotlin, that enables your android app have multiple

Gourav Khunger 8 Nov 28, 2022
Helper object for injecting typeface into various text views of android.

TypefaceHelper Helper object for injecting typeface into various text views of android. Overview We can use various custom typefaces asset for any tex

Drivemode, Inc. 105 Nov 25, 2022
A demo for Android font typeface support in React Native!

A Step-by-step Guide to a Consistent Multi-Platform Font Typeface Experience in React Native Goal Be able to use font typeface modifiers such as fontW

Jules Sam. Randolph 53 Dec 23, 2022
AndroidEssentials is an android library that creates helper functions for performing common tasks in Android

AndroidEssentials is an android library that creates helper functions for performing common tasks in Android such as managing preferences, managing files, showing alerts, showing toasts, checking user country & checking network connection of users. All the methods of the class are static and should be accessed directly from the AndroidEssentials class.

Isaac Sichangi 3 Jul 7, 2022