Android: How to Serialize a Typeface

313 views Asked by At

I want to past a Typeface through an Intent (more specifically a class containing a Typeface through an Intent) but can't because the class isn't Serializable. I tried creating a container class like so:

public class SerializableTypeface extends Typeface implments Serializable {

}

But got an error because Typeface has no default constructor. But when looking for parameters to put in the constructor I couldn't find any constructors for a Typeface.

So how do I serialize a Typeface?

2

There are 2 answers

0
Yossi Segev On

Another way to achieve this would be to store the Typeface name as a String and parse it on the "other side".

For example:

String typefaceName;

Typeface font = Typeface.createFromAsset(getContext().getAssets(), typefaceName);
0
Ben P. On

Typeface does not have any public or protected constructors. The only constructor is private, and as such you cannot extend the Typeface class.

One possibility is to store the Typeface instance in your object as a transient field, and additionally store the information needed to generate the Typeface. These fields could be parceled/serialized, and the Typeface could be lazy-loaded as needed.