public class ColorStateList extends ComplexColor implements Parcelable
View
state sets to colors.
ColorStateList
s are created from XML resource files defined in the
"color" subdirectory directory of an application's resource directory. The XML file contains
a single "selector" element with a number of "item" elements inside. For example:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="@color/sample_focused" /> <item android:state_pressed="true" android:state_enabled="false" android:color="@color/sample_disabled_pressed" /> <item android:state_enabled="false" android:color="@color/sample_disabled_not_pressed" /> <item android:color="@color/sample_default" /> </selector>This defines a set of state spec / color pairs where each state spec specifies a set of states that a view must either be in or not be in and the color specifies the color associated with that spec.
Each item defines a set of state spec and color pairs, where the state spec is a series of
attributes set to either true
or false
to represent inclusion or exclusion. If
an attribute is not specified for an item, it may be any value.
For example, the following item will be matched whenever the focused state is set; any other states may be set or unset:
<item android:state_focused="true" android:color="@color/sample_focused" />
Typically, a color state list will reference framework-defined state attributes such as
android:state_focused
or
android:state_enabled
; however, app-defined attributes may
also be used.
Note: The list of state specs will be matched against in the order that they appear in the XML file. For this reason, more-specific items should be placed earlier in the file. An item with no state spec is considered to match any set of states and is generally useful as a final item to be used as a default.
If an item with no state spec if placed before other items, those items will be ignored.
Each item must define an android:color
attribute, which may be
an HTML-style hex color, a reference to a color resource, or -- in API 23 and above -- a theme
attribute that resolves to a color.
Starting with API 23, items may optionally define an android:alpha
attribute to modify the base color's opacity. This attribute takes a either floating-point value
between 0 and 1 or a theme attribute that resolves as such. The item's overall color is
calculated by multiplying by the base color's alpha channel by the alpha
value. For
example, the following item represents the theme's accent color at 50% opacity:
<item android:state_enabled="false" android:color="?android:attr/colorAccent" android:alpha="0.5" />
For more information, see the guide to Color State List Resource.
Parcelable.ClassLoaderCreator<T>, Parcelable.Creator<T>
Modifier and Type | Field and Description |
---|---|
static Parcelable.Creator<ColorStateList> |
CREATOR |
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_ELIDE_DUPLICATES, PARCELABLE_WRITE_RETURN_VALUE
Constructor and Description |
---|
ColorStateList(int[][] states,
int[] colors)
Creates a ColorStateList that returns the specified mapping from
states to colors.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canApplyTheme()
Returns whether a theme can be applied to this color state list, which
usually indicates that the color state list has unresolved theme
attributes.
|
static ColorStateList |
createFromXml(Resources r,
XmlPullParser parser)
Deprecated.
Use #createFromXml(Resources, XmlPullParser parser, Theme)
|
static ColorStateList |
createFromXml(Resources r,
XmlPullParser parser,
Resources.Theme theme)
Creates a ColorStateList from an XML document using given a set of
Resources and a Resources.Theme . |
int |
describeContents()
Describe the kinds of special objects contained in this Parcelable
instance's marshaled representation.
|
int |
getChangingConfigurations()
Returns a mask of the configuration parameters for which this color
state list may change, requiring that it be re-created.
|
int |
getColorForState(int[] stateSet,
int defaultColor)
Return the color associated with the given set of
View states. |
int[] |
getColors()
Return the colors in this
ColorStateList . |
ConstantState<ComplexColor> |
getConstantState() |
int |
getDefaultColor()
Return the default color in this
ColorStateList . |
int[][] |
getStates()
Return the states in this
ColorStateList . |
boolean |
hasState(int state)
Returns whether the specified state is referenced in any of the state
specs contained within this ColorStateList.
|
boolean |
isOpaque()
Indicates whether this color state list is opaque, which means that every
color returned from
getColorForState(int[], int) has an alpha
value of 255. |
boolean |
isStateful()
Indicates whether this color state list contains more than one state spec
and will change color based on state.
|
ColorStateList |
obtainForTheme(Resources.Theme t)
Returns an appropriately themed color state list.
|
String |
toString()
Returns a string representation of the object.
|
static ColorStateList |
valueOf(int color) |
ColorStateList |
withAlpha(int alpha)
Creates a new ColorStateList that has the same states and colors as this
one but where each color has the specified alpha value (0-255).
|
void |
writeToParcel(Parcel dest,
int flags)
Flatten this object in to a Parcel.
|
public static final Parcelable.Creator<ColorStateList> CREATOR
public ColorStateList(int[][] states, int[] colors)
public static ColorStateList valueOf(int color)
@Deprecated public static ColorStateList createFromXml(Resources r, XmlPullParser parser) throws XmlPullParserException, IOException
r
- Resources against which the ColorStateList should be inflated.parser
- Parser for the XML document defining the ColorStateList.XmlPullParserException
IOException
public static ColorStateList createFromXml(Resources r, XmlPullParser parser, Resources.Theme theme) throws XmlPullParserException, IOException
Resources
and a Resources.Theme
.r
- Resources against which the ColorStateList should be inflated.parser
- Parser for the XML document defining the ColorStateList.theme
- Optional theme to apply to the color state list, may be
null
.XmlPullParserException
IOException
public ColorStateList withAlpha(int alpha)
alpha
- The new alpha channel value (0-255).public boolean canApplyTheme()
canApplyTheme
in class ComplexColor
public ColorStateList obtainForTheme(Resources.Theme t)
obtainForTheme
in class ComplexColor
t
- the theme to applypublic int getChangingConfigurations()
getChangingConfigurations
in class ComplexColor
ActivityInfo
ActivityInfo
public boolean isStateful()
isStateful
in class ComplexColor
getColorForState(int[], int)
public boolean isOpaque()
getColorForState(int[], int)
has an alpha
value of 255.public int getColorForState(int[] stateSet, int defaultColor)
View
states.stateSet
- an array of View
statesdefaultColor
- the color to return if there's no matching state
spec in this ColorStateList
that matches the
stateSet.ColorStateList
.public int getDefaultColor()
ColorStateList
.getDefaultColor
in class ComplexColor
ColorStateList
.public int[][] getStates()
ColorStateList
. The returned array
should not be modified.ColorStateList
public int[] getColors()
ColorStateList
. The returned array
should not be modified.ColorStateList
public boolean hasState(int state)
Any reference, either positive or negative {ex. ~R.attr.state_enabled},
will cause this method to return true
. Wildcards are not counted
as references.
state
- the state to search fortrue
if the state if referenced, false
otherwisepublic String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public ConstantState<ComplexColor> getConstantState()
getConstantState
in class ComplexColor
public int describeContents()
Parcelable
Parcelable.writeToParcel(Parcel, int)
,
the return value of this method must include the
Parcelable.CONTENTS_FILE_DESCRIPTOR
bit.describeContents
in interface Parcelable
Parcelable.CONTENTS_FILE_DESCRIPTOR
public void writeToParcel(Parcel dest, int flags)
Parcelable
writeToParcel
in interface Parcelable
dest
- The Parcel in which the object should be written.flags
- Additional flags about how the object should be written.
May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE
.