ResourcesCompat
public final class ResourcesCompat
Helper for accessing features in Resources.
Summary
Nested types |
|---|
public abstract class ResourcesCompat.FontCallbackInterface used to receive asynchronous font fetching events. |
public final class ResourcesCompat.ThemeCompatProvides backward-compatible implementations for new |
Public methods |
|
|---|---|
static void |
Clears cached values associated with the specified |
static @Nullable Typeface |
getCachedFont(@NonNull Context context, @FontRes int id)Returns a cached font Typeface associated with a particular resource ID. |
static @ColorInt int |
Returns a themed color integer associated with a particular resource ID. |
static @Nullable ColorStateList |
getColorStateList(Returns a themed color state list associated with a particular resource ID. |
static @Nullable Drawable |
getDrawable(Return a drawable object associated with a particular resource ID and styled for the specified theme. |
static @Nullable Drawable |
getDrawableForDensity(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 |
Returns a font Typeface associated with a particular resource ID. |
static void |
getFont(Returns a font Typeface associated with a particular resource ID asynchronously. |
Constants
Public methods
clearCachesForTheme
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
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 |
| Throws | |
|---|---|
android.content.res.Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
| See also | |
|---|---|
getFont |
getColor
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 |
| Returns | |
|---|---|
@ColorInt int |
A single color value in the form |
| Throws | |
|---|---|
android.content.res.Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getColorStateList
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 |
@Nullable Resources.Theme theme |
The theme used to style the color attributes, may be |
| 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
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 |
| Throws | |
|---|---|
android.content.res.Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getDrawableForDensity
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 |
@Nullable Resources.Theme theme |
The theme used to style the drawable attributes, may be |
| Throws | |
|---|---|
android.content.res.Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getFloat
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
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 |
| Throws | |
|---|---|
android.content.res.Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
| See also | |
|---|---|
getFont |
getFont
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 |
@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. |