Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

ResourcesCompat

class ResourcesCompat
kotlin.Any
   ↳ androidx.core.content.res.ResourcesCompat

Helper for accessing features in android.content.res.Resources.

Summary

Nested classes

abstract

Interface used to receive asynchronous font fetching events.

Public methods

static Int
getColor(@NonNull res: Resources, @ColorRes id: Int, @Nullable theme: Theme?)

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

static ColorStateList?
getColorStateList(@NonNull res: Resources, @ColorRes id: Int, @Nullable theme: Theme?)

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

static Drawable?
getDrawable(@NonNull res: Resources, @DrawableRes id: Int, @Nullable theme: Theme?)

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

static Drawable?
getDrawableForDensity(@NonNull res: Resources, @DrawableRes id: Int, density: Int, @Nullable theme: Theme?)

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

static Float
getFloat(@NonNull res: Resources, @DimenRes id: Int)

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

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

Returns a font Typeface associated with a particular resource ID.

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

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

Public methods

getColor

static fun getColor(@NonNull res: Resources, @ColorRes id: Int, @Nullable theme: Theme?): 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 Resources#getColor(int).

Parameters
id Resources: 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: The theme used to style the color attributes, may be null.
Return
Int: A single color value in the form 0xAARRGGBB.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist.

getColorStateList

@Nullable static fun getColorStateList(@NonNull res: Resources, @ColorRes id: Int, @Nullable theme: 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.

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

Parameters
id Resources: 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: The theme used to style the color attributes, may be null.
Return
ColorStateList?: A themed ColorStateList object containing either a single solid color or multiple colors that can be selected based on a state.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist.

getDrawable

@Nullable static fun getDrawable(@NonNull res: Resources, @DrawableRes id: Int, @Nullable theme: 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 Resources#getDrawable(int).

Parameters
id Resources: 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: The theme used to style the drawable attributes, may be null.
Return
Drawable?: Drawable An object that can be used to draw this resource.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist.

getDrawableForDensity

@Nullable static fun getDrawableForDensity(@NonNull res: Resources, @DrawableRes id: Int, density: Int, @Nullable theme: 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 Resources#getDrawable(int).

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

Parameters
id Resources: 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 Resources: The desired screen density indicated by the resource as found in android.util.DisplayMetrics.
theme Resources: The theme used to style the drawable attributes, may be null.
Return
Drawable?: Drawable An object that can be used to draw this resource.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist.

getFloat

static fun getFloat(@NonNull res: Resources, @DimenRes id: Int): Float

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

Parameters
id Resources: 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.
Return
Float: Returns the floating-point value contained in the resource.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist or is not a floating-point value.

getFont

@Nullable static fun getFont(@NonNull context: Context, @FontRes id: 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(Context, int, FontCallback, Handler) 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 Context: 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.
Return
Typeface?: A font Typeface object.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist.

getFont

static fun getFont(@NonNull context: Context, @FontRes id: Int, @NonNull fontCallback: ResourcesCompat.FontCallback, @Nullable 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 Context: 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 Context: A callback to receive async fetching of this font. The callback will be triggered on the UI thread.
handler Context: A handler for the thread the callback should be called on. If null, the callback will be called on the UI thread.
Exceptions
NotFoundException Throws NotFoundException if the given ID does not exist.