ResourcesCompat

Added in 1.1.0

public final class ResourcesCompat


Helper for accessing features in Resources.

Summary

Nested types

public abstract class ResourcesCompat.FontCallback

Interface used to receive asynchronous font fetching events.

public final class ResourcesCompat.ThemeCompat

Provides backward-compatible implementations for new Theme APIs.

Constants

static final @AnyRes int

The null resource ID.

Public methods

static void

Clears cached values associated with the specified Theme.

static @Nullable Typeface
getCachedFont(@NonNull Context context, @FontRes int id)

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

static @ColorInt int
getColor(
    @NonNull Resources res,
    @ColorRes int id,
    @Nullable Resources.Theme theme
)

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

static @Nullable ColorStateList
getColorStateList(
    @NonNull Resources res,
    @ColorRes int id,
    @Nullable Resources.Theme theme
)

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

static @Nullable Drawable
getDrawable(
    @NonNull Resources res,
    @DrawableRes int id,
    @Nullable Resources.Theme theme
)

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

static @Nullable Drawable
getDrawableForDensity(
    @NonNull Resources res,
    @DrawableRes int id,
    int density,
    @Nullable Resources.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

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

static @Nullable Typeface
getFont(@NonNull Context context, @FontRes int id)

Returns a font Typeface associated with a particular resource ID.

static void
getFont(
    @NonNull Context context,
    @FontRes int id,
    @NonNull ResourcesCompat.FontCallback fontCallback,
    @Nullable Handler handler
)

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

Constants

ID_NULL

Added in 1.3.0
public static final @AnyRes int ID_NULL = 0

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 methods

clearCachesForTheme

Added in 1.8.0
public static void clearCachesForTheme(@NonNull Resources.Theme theme)

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
@NonNull Resources.Theme theme

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

getCachedFont

Added in 1.5.0
public static @Nullable Typeface getCachedFont(@NonNull Context context, @FontRes int id)

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
@NonNull Context context

A context to retrieve the Resources from.

@FontRes int id

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
@Nullable 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
public static @ColorInt int getColor(
    @NonNull Resources res,
    @ColorRes int id,
    @Nullable Resources.Theme theme
)

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
@NonNull Resources res

resources to use for getting the color.

@ColorRes int id

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.

@Nullable Resources.Theme 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
public static @Nullable ColorStateList getColorStateList(
    @NonNull Resources res,
    @ColorRes int id,
    @Nullable Resources.Theme theme
)

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
@NonNull Resources res

resources to use for getting the color state list.

@ColorRes int id

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.

@Nullable Resources.Theme theme

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

Returns
@Nullable 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
public static @Nullable Drawable getDrawable(
    @NonNull Resources res,
    @DrawableRes int id,
    @Nullable Resources.Theme theme
)

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
@NonNull Resources res

resources to use for getting the drawable.

@DrawableRes int id

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.

@Nullable Resources.Theme theme

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

Returns
@Nullable 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
public static @Nullable Drawable getDrawableForDensity(
    @NonNull Resources res,
    @DrawableRes int id,
    int density,
    @Nullable Resources.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.

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
@NonNull Resources res

resources to use for getting the drawable.

@DrawableRes int id

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.

int density

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

@Nullable Resources.Theme theme

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

Returns
@Nullable 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
public static float getFloat(@NonNull Resources res, @DimenRes int id)

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

Parameters
@NonNull Resources res

resources to use for getting the value.

@DimenRes int id

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
public static @Nullable Typeface getFont(@NonNull Context context, @FontRes int id)

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
@NonNull Context context

A context to retrieve the Resources from.

@FontRes int id

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
@Nullable 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
public static void getFont(
    @NonNull Context context,
    @FontRes int id,
    @NonNull ResourcesCompat.FontCallback fontCallback,
    @Nullable Handler handler
)

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
@NonNull Context context

A context to retrieve the Resources from.

@FontRes int id

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.

@NonNull ResourcesCompat.FontCallback fontCallback

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

@Nullable 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.