Android APIs
public class

ColorStateList

extends Object
implements Parcelable
java.lang.Object
   ↳ android.content.res.ColorStateList

Class Overview

Lets you map View state sets to colors. ColorStateLists 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/testcolor1"/>
   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
   <item android:state_enabled="false" android:color="@color/testcolor3" />
   <item android:color="@color/testcolor5"/>
 </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. The list of state specs will be processed in order of the items in the XML 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. Note that if you have such an item before any other items in the list then any subsequent items will end up being ignored.

For more information, see the guide to Color State List Resource.

Summary

[Expand]
Inherited Constants
From interface android.os.Parcelable
Fields
public static final Creator<ColorStateList> CREATOR
Public Constructors
ColorStateList(int[][] states, int[] colors)
Creates a ColorStateList that returns the specified mapping from states to colors.
Public Methods
static ColorStateList createFromXml(Resources r, XmlPullParser parser)
This method was deprecated in API level 23. 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's marshalled 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 getDefaultColor()
Return the default color in 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.
String toString()
Returns a string containing a concise, human-readable description of this 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.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.os.Parcelable

Fields

public static final Creator<ColorStateList> CREATOR

Added in API level 1

Public Constructors

public ColorStateList (int[][] states, int[] colors)

Added in API level 1

Creates a ColorStateList that returns the specified mapping from states to colors.

Parameters
states int
colors int

Public Methods

public static ColorStateList createFromXml (Resources r, XmlPullParser parser)

Added in API level 1

This method was deprecated in API level 23.
Use #createFromXml(Resources, XmlPullParser parser, Theme)

Creates a ColorStateList from an XML document.

Parameters
r Resources: Resources against which the ColorStateList should be inflated.
parser XmlPullParser: Parser for the XML document defining the ColorStateList.
Returns
ColorStateList A new color state list.
Throws
XmlPullParserException
IOException

public static ColorStateList createFromXml (Resources r, XmlPullParser parser, Resources.Theme theme)

Added in API level 23

Creates a ColorStateList from an XML document using given a set of Resources and a Resources.Theme.

Parameters
r Resources: Resources against which the ColorStateList should be inflated.
parser XmlPullParser: Parser for the XML document defining the ColorStateList.
theme Resources.Theme: Optional theme to apply to the color state list, may be null.
Returns
ColorStateList A new color state list.
Throws
XmlPullParserException
IOException

public int describeContents ()

Added in API level 1

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

Returns
int a bitmask indicating the set of special object types marshalled by the Parcelable.

public int getChangingConfigurations ()

Added in API level 23

Returns a mask of the configuration parameters for which this color state list may change, requiring that it be re-created.

Returns
int a mask of the changing configuration parameters, as defined by ActivityInfo

See also:

public int getColorForState (int[] stateSet, int defaultColor)

Added in API level 1

Return the color associated with the given set of View states.

Parameters
stateSet int: an array of View states
defaultColor int: the color to return if there's no matching state spec in this ColorStateList that matches the stateSet.
Returns
int the color associated with that set of states in this ColorStateList.

public int getDefaultColor ()

Added in API level 1

Return the default color in this ColorStateList.

Returns
int the default color in this ColorStateList.

public boolean isOpaque ()

Added in API level 21

Indicates whether this color state list is opaque, which means that every color returned from getColorForState(int[], int) has an alpha value of 255.

Returns
boolean True if this color state list is opaque.

public boolean isStateful ()

Added in API level 1

Indicates whether this color state list contains more than one state spec and will change color based on state.

Returns
boolean True if this color state list changes color based on state, false otherwise.

public String toString ()

Added in API level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
String a printable representation of this object.

public static ColorStateList valueOf (int color)

Added in API level 1

Parameters
color int
Returns
ColorStateList A ColorStateList containing a single color.

public ColorStateList withAlpha (int alpha)

Added in API level 1

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).

Parameters
alpha int: The new alpha channel value (0-255).
Returns
ColorStateList A new color state list.

public void writeToParcel (Parcel dest, int flags)

Added in API level 1

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written.
flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.