ResourcesCompat

Added in 1.1.0

class ResourcesCompat


Helper for accessing features in Resources.

Summary

Nested types

Interface used to receive asynchronous font fetching events.

Provides backward-compatible implementations for new Theme APIs.

Constants

const @AnyRes Int

The null resource ID.

Public functions

java-static Unit

Clears cached values associated with the specified Theme.

java-static Typeface?
getCachedFont(context: Context, id: @FontRes Int)

Returns a cached font Typeface associated with a particular resource ID.

java-static @ColorInt Int
getColor(res: Resources, id: @ColorRes Int, theme: Resources.Theme?)

Returns a themed color integer associated with a particular resource ID.

java-static ColorStateList?

Returns a themed color state list associated with a particular resource ID.

java-static Drawable?

Return a drawable object associated with a particular resource ID and styled for the specified theme.

java-static Drawable?
getDrawableForDensity(
    res: Resources,
    id: @DrawableRes Int,
    density: Int,
    theme: Resources.Theme?
)

Return a drawable object associated with a particular resource ID for the given screen density in DPI and styled for the specified theme.

java-static Float

Retrieve a floating-point value for a particular resource ID.

java-static Typeface?
getFont(context: Context, id: @FontRes Int)

Returns a font Typeface associated with a particular resource ID.

java-static Unit
getFont(
    context: Context,
    id: @FontRes Int,
    fontCallback: ResourcesCompat.FontCallback,
    handler: Handler?
)

Returns a font Typeface associated with a particular resource ID asynchronously.

Constants

ID_NULL

Added in 1.3.0
const val ID_NULL = 0: @AnyRes Int

The null resource ID. This denotes an invalid resource ID that is returned by the system when a resource is not found or the value is set to @null in XML.

Public functions

clearCachesForTheme

Added in 1.8.0
java-static fun clearCachesForTheme(theme: Resources.Theme): Unit

Clears cached values associated with the specified Theme.

This method allows developers to work around issues related to stale cached resources that may occur on API level 32 and below following a call to applyStyle. If you are not explicitly calling applyStyle in your code, you probably do not need to call this method.

Starting in Android T, the Theme class correctly implements hashCode and cached values will be appropriately cleared when the contents of a Theme object change.

Parameters
theme: Resources.Theme

the theme for which associated values should be cleared from the resource cache

getCachedFont

Added in 1.5.0
java-static fun getCachedFont(context: Context, id: @FontRes Int): Typeface?

Returns a cached font Typeface associated with a particular resource ID.

This method returns non-null Typeface if the requested font is already fetched. Otherwise immediately returns null without requesting to font provider.

Prior to API level 23, font resources with more than one font in a family will only load the font closest to a regular weight typeface.

Parameters
context: Context

A context to retrieve the Resources from.

id: @FontRes Int

The desired resource identifier of a Typeface, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

Returns
Typeface?

A font Typeface object.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.

See also
getFont

getColor

Added in 1.1.0
java-static fun getColor(res: Resources, id: @ColorRes Int, theme: Resources.Theme?): @ColorInt Int

Returns a themed color integer associated with a particular resource ID. If the resource holds a complex ColorStateList, then the default color from the set is returned.

Prior to API level 23, the theme will not be applied and this method calls through to getColor.

Parameters
res: Resources

resources to use for getting the color.

id: @ColorRes Int

The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

theme: Resources.Theme?

The theme used to style the color attributes, may be null.

Returns
@ColorInt Int

A single color value in the form 0xAARRGGBB.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.

getColorStateList

Added in 1.1.0
java-static fun getColorStateList(res: Resources, id: @ColorRes Int, theme: Resources.Theme?): ColorStateList?

Returns a themed color state list associated with a particular resource ID. The resource may contain either a single raw color value or a complex ColorStateList holding multiple possible colors.

Parameters
res: Resources

resources to use for getting the color state list.

id: @ColorRes Int

The desired resource identifier of a ColorStateList, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

theme: Resources.Theme?

The theme used to style the color attributes, may be null.

Returns
ColorStateList?

A themed ColorStateList object containing either a single solid color or multiple colors that can be selected based on a state.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.

getDrawable

Added in 1.1.0
java-static fun getDrawable(res: Resources, id: @DrawableRes Int, theme: Resources.Theme?): Drawable?

Return a drawable object associated with a particular resource ID and styled for the specified theme. Various types of objects will be returned depending on the underlying resource -- for example, a solid color, PNG image, scalable image, etc.

Prior to API level 21, the theme will not be applied and this method simply calls through to getDrawable.

Parameters
res: Resources

resources to use for getting the drawable.

id: @DrawableRes Int

The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

theme: Resources.Theme?

The theme used to style the drawable attributes, may be null.

Returns
Drawable?

Drawable An object that can be used to draw this resource.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.

getDrawableForDensity

Added in 1.1.0
java-static fun getDrawableForDensity(
    res: Resources,
    id: @DrawableRes Int,
    density: Int,
    theme: Resources.Theme?
): Drawable?

Return a drawable object associated with a particular resource ID for the given screen density in DPI and styled for the specified theme.

Prior to API level 15, the theme and density will not be applied and this method simply calls through to getDrawable.

Prior to API level 21, the theme will not be applied and this method calls through to Resources#getDrawableForDensity(int, int).

Parameters
res: Resources

resources to use for getting the drawable.

id: @DrawableRes Int

The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

density: Int

The desired screen density indicated by the resource as found in DisplayMetrics.

theme: Resources.Theme?

The theme used to style the drawable attributes, may be null.

Returns
Drawable?

Drawable An object that can be used to draw this resource.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.

getFloat

Added in 1.1.0
java-static fun getFloat(res: Resources, id: @DimenRes Int): Float

Retrieve a floating-point value for a particular resource ID.

Parameters
res: Resources

resources to use for getting the value.

id: @DimenRes Int

The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

Returns
Float

Returns the floating-point value contained in the resource.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist or is not a floating-point value.

getFont

Added in 1.1.0
java-static fun getFont(context: Context, id: @FontRes Int): Typeface?

Returns a font Typeface associated with a particular resource ID.

This method will block the calling thread to retrieve the requested font, including if it is from a font provider. If you wish to not have this behavior, use getFont instead.

Prior to API level 23, font resources with more than one font in a family will only load the font closest to a regular weight typeface.

Parameters
context: Context

A context to retrieve the Resources from.

id: @FontRes Int

The desired resource identifier of a Typeface, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

Returns
Typeface?

A font Typeface object.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.

See also
getFont

getFont

Added in 1.1.0
java-static fun getFont(
    context: Context,
    id: @FontRes Int,
    fontCallback: ResourcesCompat.FontCallback,
    handler: Handler?
): Unit

Returns a font Typeface associated with a particular resource ID asynchronously.

Prior to API level 23, font resources with more than one font in a family will only load the font closest to a regular weight typeface.

Parameters
context: Context

A context to retrieve the Resources from.

id: @FontRes Int

The desired resource identifier of a Typeface, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

fontCallback: ResourcesCompat.FontCallback

A callback to receive async fetching of this font. The callback will be triggered on the UI thread.

handler: Handler?

A handler for the thread the callback should be called on. If null, the callback will be called on the UI thread.

Throws
android.content.res.Resources.NotFoundException

Throws NotFoundException if the given ID does not exist.