ResourcesCompat

public final class ResourcesCompat
extends Object

java.lang.Object
   ↳ androidx.core.content.res.ResourcesCompat


Helper for accessing features in Resources.

Summary

Nested classes

class ResourcesCompat.FontCallback

Interface used to receive asynchronous font fetching events. 

class ResourcesCompat.ThemeCompat

Provides backward-compatible implementations for new Resources.Theme APIs. 

Constants

int ID_NULL

The null resource ID.

Public methods

static Typeface getCachedFont(Context context, int id)

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

static int getColor(Resources res, int id, Resources.Theme theme)

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

static ColorStateList getColorStateList(Resources res, int id, Resources.Theme theme)

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

static Drawable getDrawable(Resources res, int id, Resources.Theme theme)

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

static Drawable getDrawableForDensity(Resources res, int id, int density, 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 getFloat(Resources res, int id)

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

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

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

static Typeface getFont(Context context, int id)

Returns a font Typeface associated with a particular resource ID.

Inherited methods

Constants

ID_NULL

public static final int ID_NULL

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.

Constant Value: 0 (0x00000000)

Public methods

getCachedFont

public static Typeface getCachedFont (Context context, 
                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
context Context: A context to retrieve the Resources from.

id 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
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getColor

public static int getColor (Resources res, 
                int id, 
                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 Resources.getColor(int).

Parameters
res Resources

id 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
int A single color value in the form 0xAARRGGBB.

Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getColorStateList

public static ColorStateList getColorStateList (Resources res, 
                int id, 
                Resources.Theme theme)

Returns a theme