to top
Android APIs
public class

Typeface

extends Object
java.lang.Object
   ↳ android.graphics.Typeface

Class Overview

The Typeface class specifies the typeface and intrinsic style of a font. This is used in the paint, along with optionally Paint settings like textSize, textSkewX, textScaleX to specify how text appears when drawn (and measured).

Summary

Constants
int BOLD
int BOLD_ITALIC
int ITALIC
int NORMAL
Fields
public static final Typeface DEFAULT The default NORMAL typeface object
public static final Typeface DEFAULT_BOLD The default BOLD typeface object.
public static final Typeface MONOSPACE The NORMAL style of the default monospace typeface.
public static final Typeface SANS_SERIF The NORMAL style of the default sans serif typeface.
public static final Typeface SERIF The NORMAL style of the default serif typeface.
Public Methods
static Typeface create(String familyName, int style)
Create a typeface object given a family name, and option style information.
static Typeface create(Typeface family, int style)
Create a typeface object that best matches the specified existing typeface and the specified Style.
static Typeface createFromAsset(AssetManager mgr, String path)
Create a new typeface from the specified font data.
static Typeface createFromFile(String path)
Create a new typeface from the specified font file.
static Typeface createFromFile(File path)
Create a new typeface from the specified font file.
static Typeface defaultFromStyle(int style)
Returns one of the default typeface objects, based on the specified style
boolean equals(Object o)
Compares this instance with the specified object and indicates if they are equal.
int getStyle()
Returns the typeface's intrinsic style attributes
int hashCode()
Returns an integer hash code for this object.
final boolean isBold()
Returns true if getStyle() has the BOLD bit set.
final boolean isItalic()
Returns true if getStyle() has the ITALIC bit set.
Protected Methods
void finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int BOLD

Added in API level 1

Constant Value: 1 (0x00000001)

public static final int BOLD_ITALIC

Added in API level 1

Constant Value: 3 (0x00000003)

public static final int ITALIC

Added in API level 1

Constant Value: 2 (0x00000002)

public static final int NORMAL

Added in API level 1

Constant Value: 0 (0x00000000)

Fields

public static final Typeface DEFAULT

Added in API level 1

The default NORMAL typeface object

public static final Typeface DEFAULT_BOLD

Added in API level 1

The default BOLD typeface object. Note: this may be not actually be bold, depending on what fonts are installed. Call getStyle() to know for sure.

public static final Typeface MONOSPACE

Added in API level 1

The NORMAL style of the default monospace typeface.

public static final Typeface SANS_SERIF

Added in API level 1

The NORMAL style of the default sans serif typeface.

public static final Typeface SERIF

Added in API level 1

The NORMAL style of the default serif typeface.

Public Methods

public static Typeface create (String familyName, int style)

Added in API level 1

Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (getStyle()) to discover what its "real" style characteristics are.

Parameters
familyName May be null. The name of the font family.
style The style (normal, bold, italic) of the typeface. e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
Returns
  • The best matching typeface.

public static Typeface create (Typeface family, int style)

Added in API level 1

Create a typeface object that best matches the specified existing typeface and the specified Style. Use this call if you want to pick a new style from the same family of an existing typeface object. If family is null, this selects from the default font's family.

Parameters
family May be null. The name of the existing type face.
style The style (normal, bold, italic) of the typeface. e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
Returns
  • The best matching typeface.

public static Typeface createFromAsset (AssetManager mgr, String path)

Added in API level 1

Create a new typeface from the specified font data.

Parameters
mgr The application's asset manager
path The file name of the font data in the assets directory
Returns
  • The new typeface.

public static Typeface createFromFile (String path)

Added in API level 4

Create a new typeface from the specified font file.

Parameters
path The full path to the font data.
Returns
  • The new typeface.

public static Typeface createFromFile (File path)

Added in API level 4

Create a new typeface from the specified font file.

Parameters
path The path to the font data.
Returns
  • The new typeface.

public static Typeface defaultFromStyle (int style)

Added in API level 1

Returns one of the default typeface objects, based on the specified style

Returns
  • the default typeface that corresponds to the style

public boolean equals (Object o)

Added in API level 1

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

Parameters
o the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public int getStyle ()

Added in API level 1

Returns the typeface's intrinsic style attributes

public int hashCode ()

Added in API level 1

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.

public final boolean isBold ()

Added in API level 1

Returns true if getStyle() has the BOLD bit set.

public final boolean isItalic ()

Added in API level 1

Returns true if getStyle() has the ITALIC bit set.

Protected Methods

protected void finalize ()

Added in API level 1

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable