Resources
public
class
Resources
extends Object
java.lang.Object | |
↳ | android.content.res.Resources |
Class for accessing an application's resources. This sits on top of the
asset manager of the application (accessible through getAssets()
) and
provides a high-level API for getting typed data from the assets.
The Android resource system keeps track of all non-code assets associated with an
application. You can use this class to access your application's resources. You can generally
acquire the Resources
instance associated with your application
with getResources()
.
The Android SDK tools compile your application's resources into the application binary
at build time. To use a resource, you must install it correctly in the source tree (inside
your project's res/
directory) and build your application. As part of the build
process, the SDK tools generate symbols for each resource, which you can use in your application
code to access the resources.
Using application resources makes it easy to update various characteristics of your application without modifying code, and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as for different languages and screen sizes). This is an important aspect of developing Android applications that are compatible on different types of devices.
After Build.VERSION_CODES#R
, Resources
must be obtained by
Activity
or Context
created with
Context.createWindowContext(int, Bundle)
.
Application#getResources()
may report wrong values in multi-window or on secondary
displays.
For more information about using resources, see the documentation about Application Resources.
Summary
Nested classes | |
---|---|
class |
Resources.NotFoundException
This exception is thrown by the resource APIs when a requested resource can not be found. |
class |
Resources.Theme
This class holds the current attribute values for a particular theme. |
Constants | |
---|---|
int |
ID_NULL
The |
Public constructors | |
---|---|
Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)
This constructor is deprecated.
Resources should not be constructed by apps.
See |
Public methods | |
---|---|
void
|
addLoaders(ResourcesLoader... loaders)
Adds a loader to the list of loaders. |
final
void
|
finishPreloading()
Called by zygote when it is done preloading resources, to change back to normal Resources operation. |
final
void
|
flushLayoutCache()
Call this to remove all cached loaded layout resources from the Resources object. |
XmlResourceParser
|
getAnimation(int id)
Return an XmlResourceParser through which you can read an animation description for the given resource ID. |
final
AssetManager
|
getAssets()
Retrieve underlying AssetManager storage for these resources. |
static
int
|
getAttributeSetSourceResId(AttributeSet set)
Returns the resource ID of the resource that was used to create this AttributeSet. |
boolean
|
getBoolean(int id)
Return a boolean associated with a particular resource ID. |
int
|
getColor(int id)
This method was deprecated
in API level 23.
Use |
int
|
getColor(int id, Resources.Theme theme)
Returns a themed color integer associated with a particular resource ID. |
ColorStateList
|
getColorStateList(int id, Resources.Theme theme)
Returns a themed color state list associated with a particular resource ID. |
ColorStateList
|
getColorStateList(int id)
This method was deprecated
in API level 23.
Use |
Configuration
|
getConfiguration()
Return the current configuration that is in effect for this resource object. |
float
|
getDimension(int id)
Retrieve a dimensional for a particular resource ID. |
int
|
getDimensionPixelOffset(int id)
Retrieve a dimensional for a particular resource ID for use as an offset in raw pixels. |
int
|
getDimensionPixelSize(int id)
Retrieve a dimensional for a particular resource ID for use as a size in raw pixels. |
DisplayMetrics
|
getDisplayMetrics()
Returns the current display metrics that are in effect for this resource object. |
Drawable
|
getDrawable(int id, Resources.Theme theme)
Return a drawable object associated with a particular resource ID and styled for the specified theme. |
Drawable
|
getDrawable(int id)
This method was deprecated
in API level 22.
Use |
Drawable
|
getDrawableForDensity(int id, int density)
This method was deprecated
in API level 22.
Use |
Drawable
|
getDrawableForDensity(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. |
float
|
getFloat(int id)
Retrieve a floating-point value for a particular resource ID. |
Typeface
|
getFont(int id)
Return the Typeface value associated with a particular resource ID. |
float
|
getFraction(int id, int base, int pbase)
Retrieve a fractional unit for a particular resource ID. |
int
|
getIdentifier(String name, String defType, String defPackage)
Return a resource identifier for the given resource name. |
int[]
|
getIntArray(int id)
Return the int array associated with a particular resource ID. |
int
|
getInteger(int id)
Return an integer associated with a particular resource ID. |
XmlResourceParser
|
getLayout(int id)
Return an XmlResourceParser through which you can read a view layout description for the given resource ID. |
Movie
|
getMovie(int id)
This method was deprecated
in API level 29.
Prefer |
String
|
getQuantityString(int id, int quantity, Object... formatArgs)
Formats the string necessary for grammatically correct pluralization of the given resource ID for the given quantity, using the given arguments. |
String
|
getQuantityString(int id, int quantity)
Returns the string necessary for grammatically correct pluralization of the given resource ID for the given quantity. |
CharSequence
|
getQuantityText(int id, int quantity)
Returns the character sequence necessary for grammatically correct pluralization of the given resource ID for the given quantity. |
String
|
getResourceEntryName(int resid)
Return the entry name for a given resource identifier. |
String
|
getResourceName(int resid)
Return the full name for a given resource identifier. |
String
|
getResourcePackageName(int resid)
Return the package name for a given resource identifier. |
String
|
getResourceTypeName(int resid)
Return the type name for a given resource identifier. |
String
|
getString(int id)
Return the string value associated with a particular resource ID. It will be stripped of any styled text information. |
String
|
getString(int id, Object... formatArgs)
Return the string value associated with a particular resource ID,
substituting the format arguments as defined in |
String[]
|
getStringArray(int id)
Return the string array associated with a particular resource ID. |
static
Resources
|
getSystem()
Return a global shared Resources object that provides access to only system resources (no application resources), is not configured for the current screen (can not use dimension units, does not change based on orientation, etc), and is not affected by Runtime Resource Overlay. |
CharSequence
|
getText(int id, CharSequence def)
Return the string value associated with a particular resource ID. |
CharSequence
|
getText(int id)
Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled. |
CharSequence[]
|
getTextArray(int id)
Return the styled text array associated with a particular resource ID. |
void
|
getValue(String name, TypedValue outValue, boolean resolveRefs)
Return the raw data associated with a particular resource ID. |
void
|
getValue(int id, TypedValue outValue, boolean resolveRefs)
Return the raw data associated with a particular resource ID. |
void
|
getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs)
Get the raw value associated with a resource with associated density. |
XmlResourceParser
|
getXml(int id)
Return an XmlResourceParser through which you can read a generic XML resource for the given resource ID. |
final
Resources.Theme
|
newTheme()
Generate a new Theme object for this set of Resources. |
TypedArray
|
obtainAttributes(AttributeSet set, int[] attrs)
Retrieve a set of basic attribute values from an AttributeSet, not performing styling of them using a theme and/or style resources. |
TypedArray
|
obtainTypedArray(int id)
Return an array of heterogeneous values. |
InputStream
|
openRawResource(int id, TypedValue value)
Open a data stream for reading a raw resource. |
InputStream
|
openRawResource(int id)
Open a data stream for reading a raw resource. |
AssetFileDescriptor
|
openRawResourceFd(int id)
Open a file descriptor for reading a raw resource. |
void
|
parseBundleExtra(String tagName, AttributeSet attrs, Bundle outBundle)
Parse a name/value pair out of an XML tag holding that data. |
void
|
parseBundleExtras(XmlResourceParser parser, Bundle outBundle)
Parse a series of |
static
void
|
registerResourcePaths(String uniqueId, ApplicationInfo appInfo)
Register the resources paths of a package (e.g. a shared library). |
void
|
removeLoaders(ResourcesLoader... loaders)
Removes loaders from the list of loaders. |
void
|
updateConfiguration(Configuration config, DisplayMetrics metrics)
This method was deprecated
in API level 25.
See |
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 constructors
Resources
public Resources (AssetManager assets, DisplayMetrics metrics, Configuration config)
This constructor is deprecated.
Resources should not be constructed by apps.
See Context.createConfigurationContext(Configuration)
.
Create a new Resources object on top of an existing set of assets in an AssetManager.
Parameters | |
---|---|
assets |
AssetManager : Previously created AssetManager. |
metrics |
DisplayMetrics : Current display metrics to consider when
selecting/computing resource values. |
config |
Configuration : Desired device configuration to consider when
selecting/computing resource values (optional). |
Public methods
addLoaders
public void addLoaders (ResourcesLoader... loaders)
Adds a loader to the list of loaders. If the loader is already present in the list, the list will not be modified.
This should only be called from the UI thread to avoid lock contention when propagating loader changes.
Parameters | |
---|---|
loaders |
ResourcesLoader : the loaders to add
This value cannot be null . |
finishPreloading
public final void finishPreloading ()
Called by zygote when it is done preloading resources, to change back to normal Resources operation.
flushLayoutCache
public final void flushLayoutCache ()
Call this to remove all cached loaded layout resources from the Resources object. Only intended for use with performance testing tools.
getAnimation
public XmlResourceParser getAnimation (int id)
Return an XmlResourceParser through which you can read an animation description for the given resource ID. This parser has limited functionality -- in particular, you can't change its input, and only the high-level events are available.
This function is really a simple wrapper for calling
getXml(int)
with an animation resource.
Parameters | |
---|---|
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. |
Returns | |
---|---|
XmlResourceParser |
A new parser object through which you can read
the XML data.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getAssets
public final AssetManager getAssets ()
Retrieve underlying AssetManager storage for these resources.
Returns | |
---|---|
AssetManager |
getAttributeSetSourceResId
public static int getAttributeSetSourceResId (AttributeSet set)
Returns the resource ID of the resource that was used to create this AttributeSet.
Parameters | |
---|---|
set |
AttributeSet : AttributeSet for which we want to find the source.
This value may be null . |
Returns | |
---|---|
int |
The resource ID for the source that is backing the given AttributeSet or
Resources#ID_NULL if the AttributeSet is null . |
getBoolean
public boolean getBoolean (int id)
Return a boolean associated with a particular resource ID. This can be used with any integral resource value, and will return true if it is non-zero.
Parameters | |
---|---|
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. |
Returns | |
---|---|
boolean |
Returns the boolean value contained in the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getColor
public int getColor (int id)
This method was deprecated
in API level 23.
Use getColor(int, android.content.res.Resources.Theme)
instead.
Returns a color integer associated with a particular resource ID. If the
resource holds a complex ColorStateList
, then the default color
from the set is returned.
Parameters | |
---|---|
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. |
Returns | |
---|---|
int |
A single color value in the form 0xAARRGGBB. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getColor
public int getColor (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.
Parameters | |
---|---|
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 ColorStateList getColorStateList (int id, 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 | |
---|---|
id |
int : 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.Theme : The theme used to style the color attributes, may be
null . |
Returns | |
---|---|
ColorStateList |
A themed ColorStateList object containing either a single solid
color or multiple colors that can be selected based on a state.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getColorStateList
public ColorStateList getColorStateList (int id)
This method was deprecated
in API level 23.
Use getColorStateList(int, android.content.res.Resources.Theme)
instead.
Returns a 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 | |
---|---|
id |
int : 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. |
Returns | |
---|---|
ColorStateList |
A ColorStateList object containing either a single solid color
or multiple colors that can be selected based on a state.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getConfiguration
public Configuration getConfiguration ()
Return the current configuration that is in effect for this resource object. The returned object should be treated as read-only.
Returns | |
---|---|
Configuration |
The resource's current configuration. |
getDimension
public float getDimension (int id)
Retrieve a dimensional for a particular resource ID. Unit
conversions are based on the current DisplayMetrics
associated
with the resources.
Parameters | |
---|---|
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. |
Returns | |
---|---|
float |
Resource dimension value multiplied by the appropriate metric to convert to pixels. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getDimensionPixelOffset
public int getDimensionPixelOffset (int id)
Retrieve a dimensional for a particular resource ID for use
as an offset in raw pixels. This is the same as
getDimension(int)
, except the returned value is converted to
integer pixels for you. An offset conversion involves simply
truncating the base value to an integer.
Parameters | |
---|---|
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. |
Returns | |
---|---|
int |
Resource dimension value multiplied by the appropriate metric and truncated to integer pixels. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getDimensionPixelSize
public int getDimensionPixelSize (int id)
Retrieve a dimensional for a particular resource ID for use
as a size in raw pixels. This is the same as
getDimension(int)
, except the returned value is converted to
integer pixels for use as a size. A size conversion involves
rounding the base value, and ensuring that a non-zero base value
is at least one pixel in size.
Parameters | |
---|---|
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. |
Returns | |
---|---|
int |
Resource dimension value multiplied by the appropriate metric and truncated to integer pixels. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getDisplayMetrics
public DisplayMetrics getDisplayMetrics ()
Returns the current display metrics that are in effect for this resource object. The returned object should be treated as read-only.
Note that the reported value may be different than the window this application is interested in.
The best practices is to obtain metrics from
WindowManager#getCurrentWindowMetrics()
for window bounds. The value obtained from
this API may be wrong if Context#getResources()
is not from a UiContext
.
For example, use the DisplayMetrics
obtained from Application#getResources()
to build Activity
UI elements especially when the
Activity
is in the multi-window mode or on the secondary Display
.
Returns | |
---|---|
DisplayMetrics |
The resource's current display metrics. |
getDrawable
public Drawable getDrawable (int id, 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 | |
---|---|
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 drawable attributes, may be null . |
Returns | |
---|---|
Drawable |
Drawable An object that can be used to draw this resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getDrawable
public Drawable getDrawable (int id)
This method was deprecated
in API level 22.
Use getDrawable(int, android.content.res.Resources.Theme)
instead.
Return a drawable object associated with a particular resource ID. Various types of objects will be returned depending on the underlying resource -- for example, a solid color, PNG image, scalable image, etc. The Drawable API hides these implementation details.
Note: Prior to
Build.VERSION_CODES.JELLY_BEAN
, this function
would not correctly retrieve the final configuration density when
the resource ID passed here is an alias to another Drawable resource.
This means that if the density configuration of the alias resource
is different than the actual resource, the density of the returned
Drawable would be incorrect, resulting in bad scaling. To work
around this, you can instead manually resolve the aliased reference
by using getValue(int, android.util.TypedValue, boolean)
and passing
true
for resolveRefs
. The resulting
TypedValue#resourceId
value may be passed to this method.
Note: To obtain a themed drawable, use
Context.getDrawable(int)
or getDrawable(int, android.content.res.Resources.Theme)
passing the desired theme.
Parameters | |
---|---|
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. |
Returns | |
---|---|
Drawable |
Drawable An object that can be used to draw this resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getDrawableForDensity
public Drawable getDrawableForDensity (int id, int density)
This method was deprecated
in API level 22.
Use getDrawableForDensity(int, int, android.content.res.Resources.Theme)
instead.
Return a drawable object associated with a particular resource ID for the given screen density in DPI. This will set the drawable's density to be the device's density multiplied by the ratio of actual drawable density to requested density. This allows the drawable to be scaled up to the correct size if needed. Various types of objects will be returned depending on the underlying resource -- for example, a solid color, PNG image, scalable image, etc. The Drawable API hides these implementation details.
Note: To obtain a themed drawable, use
Context.getDrawable(int)
or getDrawableForDensity(int, int, android.content.res.Resources.Theme)
passing the desired
theme.
Parameters | |
---|---|
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. |
density |
int : the desired screen density indicated by the resource as
found in DisplayMetrics . A value of 0 means to use the
density returned from getConfiguration() .
This is equivalent to calling getDrawable(int) . |
Returns | |
---|---|
Drawable |
Drawable An object that can be used to draw this resource.
This value may be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getDrawableForDensity
public Drawable getDrawableForDensity (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.
Parameters | |
---|---|
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. |
density |
int : The desired screen density indicated by the resource as
found in DisplayMetrics . A value of 0 means to use the
density returned from getConfiguration() .
This is equivalent to calling getDrawable(int, android.content.res.Resources.Theme) . |
theme |
Resources.Theme : The theme used to style the drawable attributes, may be null if the
drawable cannot be decoded. |
Returns | |
---|---|
Drawable |
Drawable An object that can be used to draw this resource.
This value may be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getFloat
public float getFloat (int id)
Retrieve a floating-point value for a particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
float |
Returns the floating-point value contained in the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist or is not a floating-point value. |
getFont
public Typeface getFont (int id)
Return the Typeface value associated with a particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
Typeface |
Typeface The Typeface data associated with the resource.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getFraction
public float getFraction (int id, int base, int pbase)
Retrieve a fractional unit for a particular resource ID.
Parameters | |
---|---|
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. |
base |
int : The base value of this fraction. In other words, a
standard fraction is multiplied by this value. |
pbase |
int : The parent base value of this fraction. In other
words, a parent fraction (nn%p) is multiplied by this
value. |
Returns | |
---|---|
float |
Attribute fractional value multiplied by the appropriate base value. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getIdentifier
public int getIdentifier (String name, String defType, String defPackage)
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Parameters | |
---|---|
name |
String : The name of the desired resource. |
defType |
String : Optional default resource type to find, if "type/" is
not included in the name. Can be null to require an
explicit type. |
defPackage |
String : Optional default package to find, if "package:" is
not included in the name. Can be null to require an
explicit package. |
Returns | |
---|---|
int |
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.) |
getIntArray
public int[] getIntArray (int id)
Return the int array associated with a particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
int[] |
The int array associated with the resource.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getInteger
public int getInteger (int id)
Return an integer associated with a particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
int |
Returns the integer value contained in the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getLayout
public XmlResourceParser getLayout (int id)
Return an XmlResourceParser through which you can read a view layout description for the given resource ID. This parser has limited functionality -- in particular, you can't change its input, and only the high-level events are available.
This function is really a simple wrapper for calling
getXml(int)
with a layout resource.
Parameters | |
---|---|
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. |
Returns | |
---|---|
XmlResourceParser |
A new parser object through which you can read
the XML data.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getMovie
public Movie getMovie (int id)
This method was deprecated
in API level 29.
Prefer AnimatedImageDrawable
.
Return a movie object associated with the particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
Movie |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getQuantityString
public String getQuantityString (int id, int quantity, Object... formatArgs)
Formats the string necessary for grammatically correct pluralization of the given resource ID for the given quantity, using the given arguments. Note that the string is selected based solely on grammatical necessity, and that such rules differ between languages. Do not assume you know which string will be returned for a given quantity. See String Resources for more detail.
Substitution of format arguments works as if using
Formatter
and String.format(String, Object)
.
The resulting string will be stripped of any styled text information.
Parameters | |
---|---|
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. |
quantity |
int : The number used to get the correct string for the current language's
plural rules. |
formatArgs |
Object : The format arguments that will be used for substitution. |
Returns | |
---|---|
String |
String The string data associated with the resource,
stripped of styled text information.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getQuantityString
public String getQuantityString (int id, int quantity)
Returns the string necessary for grammatically correct pluralization of the given resource ID for the given quantity. Note that the string is selected based solely on grammatical necessity, and that such rules differ between languages. Do not assume you know which string will be returned for a given quantity. See String Resources for more detail.
Parameters | |
---|---|
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. |
quantity |
int : The number used to get the correct string for the current language's
plural rules. |
Returns | |
---|---|
String |
String The string data associated with the resource,
stripped of styled text information.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getQuantityText
public CharSequence getQuantityText (int id, int quantity)
Returns the character sequence necessary for grammatically correct pluralization of the given resource ID for the given quantity. Note that the character sequence is selected based solely on grammatical necessity, and that such rules differ between languages. Do not assume you know which string will be returned for a given quantity. See String Resources for more detail.
Parameters | |
---|---|
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. |
quantity |
int : The number used to get the correct string for the current language's
plural rules. |
Returns | |
---|---|
CharSequence |
CharSequence The string data associated with the resource, plus
possibly styled text information.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getResourceEntryName
public String getResourceEntryName (int resid)
Return the entry name for a given resource identifier.
Parameters | |
---|---|
resid |
int : The resource identifier whose entry name is to be
retrieved. |
Returns | |
---|---|
String |
A string holding the entry name of the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getResourceName
public String getResourceName (int resid)
Return the full name for a given resource identifier. This name is a single string of the form "package:type/entry".
Parameters | |
---|---|
resid |
int : The resource identifier whose name is to be retrieved. |
Returns | |
---|---|
String |
A string holding the name of the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getResourcePackageName
public String getResourcePackageName (int resid)
Return the package name for a given resource identifier.
Parameters | |
---|---|
resid |
int : The resource identifier whose package name is to be
retrieved. |
Returns | |
---|---|
String |
A string holding the package name of the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getResourceTypeName
public String getResourceTypeName (int resid)
Return the type name for a given resource identifier.
Parameters | |
---|---|
resid |
int : The resource identifier whose type name is to be
retrieved. |
Returns | |
---|---|
String |
A string holding the type name of the resource. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getString
public String getString (int id)
Return the string value associated with a particular resource ID. It will be stripped of any styled text information.
Parameters | |
---|---|
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. |
Returns | |
---|---|
String |
String The string data associated with the resource,
stripped of styled text information.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getString
public String getString (int id, Object... formatArgs)
Return the string value associated with a particular resource ID,
substituting the format arguments as defined in Formatter
and String.format(String, Object)
. It will be stripped of any styled text
information.
Parameters | |
---|---|
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. |
formatArgs |
Object : The format arguments that will be used for substitution. |
Returns | |
---|---|
String |
String The string data associated with the resource,
stripped of styled text information.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getStringArray
public String[] getStringArray (int id)
Return the string array associated with a particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
String[] |
The string array associated with the resource.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getSystem
public static Resources getSystem ()
Return a global shared Resources object that provides access to only system resources (no application resources), is not configured for the current screen (can not use dimension units, does not change based on orientation, etc), and is not affected by Runtime Resource Overlay.
Returns | |
---|---|
Resources |
getText
public CharSequence getText (int id, CharSequence def)
Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled.
Parameters | |
---|---|
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. |
def |
CharSequence : The default CharSequence to return. |
Returns | |
---|---|
CharSequence |
CharSequence The string data associated with the resource, plus possibly styled text information, or def if id is 0 or not found. |
getText
public CharSequence getText (int id)
Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled.
Parameters | |
---|---|
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. |
Returns | |
---|---|
CharSequence |
CharSequence The string data associated with the resource, plus
possibly styled text information.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getTextArray
public CharSequence[] getTextArray (int id)
Return the styled text array associated with a particular resource ID.
Parameters | |
---|---|
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. |
Returns | |
---|---|
CharSequence[] |
The styled text array associated with the resource.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getValue
public void getValue (String name, TypedValue outValue, boolean resolveRefs)
Return the raw data associated with a particular resource ID. See getIdentifier() for information on how names are mapped to resource IDs, and getString(int) for information on how string resources are retrieved.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Parameters | |
---|---|
name |
String : The name of the desired resource. This is passed to
getIdentifier() with a default type of "string". |
outValue |
TypedValue : Object in which to place the resource data. |
resolveRefs |
boolean : If true, a resource that is a reference to another
resource will be followed so that you receive the
actual final resource data. If false, the TypedValue
will be filled in with the reference itself. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getValue
public void getValue (int id, TypedValue outValue, boolean resolveRefs)
Return the raw data associated with a particular resource ID.
Parameters | |
---|---|
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. |
outValue |
TypedValue : Object in which to place the resource data. |
resolveRefs |
boolean : If true, a resource that is a reference to another
resource will be followed so that you receive the
actual final resource data. If false, the TypedValue
will be filled in with the reference itself. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
getValueForDensity
public void getValueForDensity (int id, int density, TypedValue outValue, boolean resolveRefs)
Get the raw value associated with a resource with associated density.
Parameters | |
---|---|
id |
int : resource identifier |
density |
int : density in DPI |
outValue |
TypedValue |
resolveRefs |
boolean : If true, a resource that is a reference to another
resource will be followed so that you receive the actual final
resource data. If false, the TypedValue will be filled in with
the reference itself. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
getXml
public XmlResourceParser getXml (int id)
Return an XmlResourceParser through which you can read a generic XML resource for the given resource ID.
The XmlPullParser implementation returned here has some limited functionality. In particular, you can't change its input, and only high-level parsing events are available (since the document was pre-parsed for you at build time, which involved merging text and stripping comments).
Parameters | |
---|---|
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. |
Returns | |
---|---|
XmlResourceParser |
A new parser object through which you can read
the XML data.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
See also:
newTheme
public final Resources.Theme newTheme ()
Generate a new Theme object for this set of Resources. It initially starts out empty.
Returns | |
---|---|
Resources.Theme |
Theme The newly created Theme container. |
obtainAttributes
public TypedArray obtainAttributes (AttributeSet set, int[] attrs)
Retrieve a set of basic attribute values from an AttributeSet, not performing styling of them using a theme and/or style resources.
Parameters | |
---|---|
set |
AttributeSet : The current attribute values to retrieve. |
attrs |
int : The specific attributes to be retrieved. These attribute IDs must be sorted in
ascending order. |
Returns | |
---|---|
TypedArray |
Returns a TypedArray holding an array of the attribute values.
Be sure to call TypedArray.recycle()
when done with it. |
obtainTypedArray
public TypedArray obtainTypedArray (int id)
Return an array of heterogeneous values.
Parameters | |
---|---|
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. |
Returns | |
---|---|
TypedArray |
Returns a TypedArray holding an array of the array values.
Be sure to call TypedArray.recycle()
when done with it.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
openRawResource
public InputStream openRawResource (int id, TypedValue value)
Open a data stream for reading a raw resource. This can only be used with resources whose value is the name of an asset file -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color resources.
Parameters | |
---|---|
id |
int : The resource identifier to open, as generated by the aapt tool. |
value |
TypedValue : The TypedValue object to hold the resource information. |
Returns | |
---|---|
InputStream |
InputStream Access to the resource data.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
openRawResource
public InputStream openRawResource (int id)
Open a data stream for reading a raw resource. This can only be used with resources whose value is the name of an asset files -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color resources.
Parameters | |
---|---|
id |
int : The resource identifier to open, as generated by the aapt tool. |
Returns | |
---|---|
InputStream |
InputStream Access to the resource data.
This value cannot be null . |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
openRawResourceFd
public AssetFileDescriptor openRawResourceFd (int id)
Open a file descriptor for reading a raw resource. This can only be used with resources whose value is the name of an asset files -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color resources.
This function only works for resources that are stored in the package as uncompressed data, which typically includes things like mp3 files and png images.
Parameters | |
---|---|
id |
int : The resource identifier to open, as generated by the aapt tool. |
Returns | |
---|---|
AssetFileDescriptor |
AssetFileDescriptor A new file descriptor you can use to read the resource. This includes the file descriptor itself, as well as the offset and length of data where the resource appears in the file. A null is returned if the file exists but is compressed. |
Throws | |
---|---|
Resources.NotFoundException |
Throws NotFoundException if the given ID does not exist. |
parseBundleExtra
public void parseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle)
Parse a name/value pair out of an XML tag holding that data. The
AttributeSet must be holding the data defined by
R.styleable.Extra
. The following value types are supported:
-
TypedValue#TYPE_STRING
:Bundle.putCharSequence()
-
TypedValue#TYPE_INT_BOOLEAN
:Bundle.putBoolean()
-
TypedValue#TYPE_FIRST_INT
-TypedValue#TYPE_LAST_INT
:Bundle.putBoolean()
-
TypedValue#TYPE_FLOAT
:Bundle.putFloat()
Parameters | |
---|---|
tagName |
String : The name of the tag these attributes come from; this is
only used for reporting error messages. |
attrs |
AttributeSet : The attributes from which to retrieve the name/value pair. |
outBundle |
Bundle : The Bundle in which to place the parsed value. |
Throws | |
---|---|
XmlPullParserException |
If the attributes are not valid. |
parseBundleExtras
public void parseBundleExtras (XmlResourceParser parser, Bundle outBundle)
Parse a series of <extra>
tags from
an XML file. You call this when you are at the parent tag of the
extra tags, and it will return once all of the child tags have been parsed.
This will call parseBundleExtra(String, AttributeSet, Bundle)
for each extra tag encountered.
Parameters | |
---|---|
parser |
XmlResourceParser : The parser from which to retrieve the extras. |
outBundle |
Bundle : A Bundle in which to place all parsed extras. |
Throws | |
---|---|
|
org.xmlpull.v1.XmlPullParserException |
|
java.io.IOException |
IOException |
|
XmlPullParserException |
registerResourcePaths
public static void registerResourcePaths (String uniqueId, ApplicationInfo appInfo)
Register the resources paths of a package (e.g. a shared library). This will collect the package resources' paths from its ApplicationInfo and add them to all existing and future contexts while the application is running. A second call with the same uniqueId is a no-op. The paths are not persisted during application restarts. The application is responsible for calling the API again if this happens.
Parameters | |
---|---|
uniqueId |
String : The unique id for the ApplicationInfo object, to detect and ignore repeated
API calls.
This value cannot be null . |
appInfo |
ApplicationInfo : The ApplicationInfo that contains resources paths of the package.
This value cannot be null . |
removeLoaders
public void removeLoaders (ResourcesLoader... loaders)
Removes loaders from the list of loaders. If the loader is not present in the list, the list will not be modified.
This should only be called from the UI thread to avoid lock contention when propagating loader changes.
Parameters | |
---|---|
loaders |
ResourcesLoader : the loaders to remove
This value cannot be null . |
updateConfiguration
public void updateConfiguration (Configuration config, DisplayMetrics metrics)
This method was deprecated
in API level 25.
See Context.createConfigurationContext(Configuration)
.
Store the newly updated configuration.
Parameters | |
---|---|
config |
Configuration |
metrics |
DisplayMetrics |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-06-18 UTC.