# ResourcesCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/res/ResourcesCompat.java+class:androidx.core.content.res.ResourcesCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public final class ResourcesCompat
```

<br />

*** ** * ** ***

Helper for accessing features in `https://developer.android.com/reference/android/content/res/Resources.html`.

## Summary

| ### Nested types |
|---|
| `public abstract class https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat.FontCallback` Interface used to receive asynchronous font fetching events. |
| `public final class https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat.ThemeCompat` Provides backward-compatible implementations for new `https://developer.android.com/reference/android/content/res/Resources.Theme.html` APIs. |

| ### Constants |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/AnyRes int` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#ID_NULL() = 0` The `null` resource ID. |

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#clearCachesForTheme(android.content.res.Resources.Theme)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme)` Clears cached values associated with the specified `https://developer.android.com/reference/android/content/res/Resources.Theme.html`. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Typeface.html` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getCachedFont(android.content.Context,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/FontRes int id)` Returns a cached font Typeface associated with a particular resource ID. |
| `static @https://developer.android.com/reference/androidx/annotation/ColorInt int` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getColor(android.content.res.Resources,int,android.content.res.Resources.Theme)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res, @https://developer.android.com/reference/androidx/annotation/ColorRes int id, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme )` Returns a themed color integer associated with a particular resource ID. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/ColorStateList.html` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getColorStateList(android.content.res.Resources,int,android.content.res.Resources.Theme)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res, @https://developer.android.com/reference/androidx/annotation/ColorRes int id, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme )` Returns a themed color state list associated with a particular resource ID. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/drawable/Drawable.html` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getDrawable(android.content.res.Resources,int,android.content.res.Resources.Theme)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res, @https://developer.android.com/reference/androidx/annotation/DrawableRes int id, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme )` Return a drawable object associated with a particular resource ID and styled for the specified theme. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/drawable/Drawable.html` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getDrawableForDensity(android.content.res.Resources,int,int,android.content.res.Resources.Theme)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res, @https://developer.android.com/reference/androidx/annotation/DrawableRes int id, int density, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html 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` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getFloat(android.content.res.Resources,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res, @https://developer.android.com/reference/androidx/annotation/DimenRes int id)` Retrieve a floating-point value for a particular resource ID. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Typeface.html` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getFont(android.content.Context,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/FontRes int id)` Returns a font Typeface associated with a particular resource ID. |
| `static void` | `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getFont(android.content.Context,int,androidx.core.content.res.ResourcesCompat.FontCallback,android.os.Handler)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/FontRes int id, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat.FontCallback fontCallback, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Handler.html handler )` Returns a font Typeface associated with a particular resource ID asynchronously. |

## Constants

### ID_NULL

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#1.8.0)

```
public static void clearCachesForTheme(@NonNull Resources.Theme theme)
```

Clears cached values associated with the specified `https://developer.android.com/reference/android/content/res/Resources.Theme.html`.

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 `https://developer.android.com/reference/android/content/res/Resources.Theme.html#applyStyle(int, boolean)`. 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 `https://developer.android.com/reference/android/content/res/Resources.Theme.html#hashCode()` and cached values will be appropriately cleared when the contents of a Theme object change.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme` | the theme for which associated values should be cleared from the resource cache |

### getCachedFont

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to retrieve the Resources from. |
| `@https://developer.android.com/reference/androidx/annotation/FontRes int id` | The desired resource identifier of a `https://developer.android.com/reference/android/graphics/Typeface.html`, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Typeface.html` | A font Typeface object. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getFont(android.content.Context,int,androidx.core.content.res.ResourcesCompat.FontCallback,android.os.Handler)` |   |

### getColor

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/android/content/res/ColorStateList.html`, then the default color from the set is returned.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res` | resources to use for getting the color. |
| `@https://developer.android.com/reference/androidx/annotation/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. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme` | The theme used to style the color attributes, may be `null`. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/ColorInt int` | A single color value in the form `0xAARRGGBB`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |

### getColorStateList

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/android/content/res/ColorStateList.html` holding multiple possible colors.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res` | resources to use for getting the color state list. |
| `@https://developer.android.com/reference/androidx/annotation/ColorRes int id` | The desired resource identifier of a `https://developer.android.com/reference/android/content/res/ColorStateList.html`, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme` | The theme used to style the color attributes, may be `null`. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/ColorStateList.html` | A themed ColorStateList object containing either a single solid color or multiple colors that can be selected based on a state. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |

### getDrawable

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res` | resources to use for getting the drawable. |
| `@https://developer.android.com/reference/androidx/annotation/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. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme` | The theme used to style the drawable attributes, may be `null`. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/drawable/Drawable.html` | Drawable An object that can be used to draw this resource. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |

### getDrawableForDensity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res` | resources to use for getting the drawable. |
| `@https://developer.android.com/reference/androidx/annotation/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 `https://developer.android.com/reference/android/util/DisplayMetrics.html`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/res/Resources.Theme.html theme` | The theme used to style the drawable attributes, may be `null`. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/drawable/Drawable.html` | Drawable An object that can be used to draw this resource. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |

### getFloat

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static float getFloat(@NonNull Resources res, @DimenRes int id)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/res/Resources.html res` | resources to use for getting the value. |
| `@https://developer.android.com/reference/androidx/annotation/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 |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist or is not a floating-point value. |

### getFont

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getFont(android.content.Context,int,androidx.core.content.res.ResourcesCompat.FontCallback,android.os.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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to retrieve the Resources from. |
| `@https://developer.android.com/reference/androidx/annotation/FontRes int id` | The desired resource identifier of a `https://developer.android.com/reference/android/graphics/Typeface.html`, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Typeface.html` | A font Typeface object. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat#getFont(android.content.Context,int,androidx.core.content.res.ResourcesCompat.FontCallback,android.os.Handler)` |   |

### getFont

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to retrieve the Resources from. |
| `@https://developer.android.com/reference/androidx/annotation/FontRes int id` | The desired resource identifier of a `https://developer.android.com/reference/android/graphics/Typeface.html`, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/content/res/ResourcesCompat.FontCallback fontCallback` | A callback to receive async fetching of this font. The callback will be triggered on the UI thread. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Handler.html handler` | A handler for the thread the callback should be called on. If null, the callback will be called on the UI thread. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | Throws NotFoundException if the given ID does not exist. |