Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [XML Attrs](https://developer.android.com/reference/android/content/res/ColorStateList#lattrs) \| [Inherited Constants](https://developer.android.com/reference/android/content/res/ColorStateList#inhconstants) \| [Fields](https://developer.android.com/reference/android/content/res/ColorStateList#lfields) \| [Ctors](https://developer.android.com/reference/android/content/res/ColorStateList#pubctors) \| [Methods](https://developer.android.com/reference/android/content/res/ColorStateList#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/content/res/ColorStateList#inhmethods)

# ColorStateList

*** ** * ** ***

` public class ColorStateList `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/android/os/Parcelable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.content.res.ColorStateList |

<br />

*** ** * ** ***

Lets you map `https://developer.android.com/reference/android/view/View` state sets to colors.

`https://developer.android.com/reference/android/content/res/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.

### State specs

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 `https://developer.android.com/reference/android/R.attr#state_focused` or `https://developer.android.com/reference/android/R.attr#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 is placed before other items, those items will be ignored.

### Item attributes

Each item must define an `https://developer.android.com/reference/android/R.attr#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 `https://developer.android.com/reference/android/R.attr#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" />
```

Starting with API 31, items may optionally define an `https://developer.android.com/reference/android/R.attr#lStar` attribute to modify the base color's perceptual luminance. This attribute takes either a floating-point value between 0 and 100 or a theme attribute that resolves as such. The item's overall color is calculated by converting the base color to an accessibility friendly color space and setting its L\* to the value specified on the `lStar` attribute. For example, the following item represents the theme's accent color at 50% perceptual luminance:

```
<item android:state_enabled="false"
        android:color="?android:attr/colorAccent"
        android:lStar="50" />
```

### Developer guide

For more information, see the guide to [Color State List Resource](https://developer.android.com/guide/topics/resources/color-list-resource).

## Summary

| ### XML attributes ||
|---|---|
| [`android:alpha`](https://developer.android.com/reference/android/content/res/ColorStateList#attr_android:alpha) | Alpha multiplier applied to the base color. |
| [`android:color`](https://developer.android.com/reference/android/content/res/ColorStateList#attr_android:color) | Base color for this state. |
| [`android:lStar`](https://developer.android.com/reference/android/content/res/ColorStateList#attr_android:lStar) | Perceptual luminance applied to the base color. |

### Inherited constants

From interface `https://developer.android.com/reference/android/os/Parcelable` `int` `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR`

Descriptor bit used with `https://developer.android.com/reference/android/os/Parcelable#describeContents()`: indicates that the Parcelable object's flattened representation includes a file descriptor.
`int` `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`

Flag for use with `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`: the object being written is a return value, that is the result of a function such as "`Parcelable someFunction()`", "`void someFunction(out Parcelable)`", or "`void someFunction(inout Parcelable)`".

| ### Fields ||
|---|---|
| ` public static final https://developer.android.com/reference/android/os/Parcelable.Creator<https://developer.android.com/reference/android/content/res/ColorStateList>` | `https://developer.android.com/reference/android/content/res/ColorStateList#CREATOR` <br /> |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/content/res/ColorStateList#ColorStateList(int[][],%20int[])(int[][] states, int[] colors) ` Creates a ColorStateList that returns the specified mapping from states to colors. |

| ### Public methods ||
|---|---|
| ` static https://developer.android.com/reference/android/content/res/ColorStateList` | ` https://developer.android.com/reference/android/content/res/ColorStateList#createFromXml(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser)(https://developer.android.com/reference/android/content/res/Resources r, https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser parser) ` *This method was deprecated in API level 23. Use #createFromXml(Resources, XmlPullParser parser, Theme)* |
| ` static https://developer.android.com/reference/android/content/res/ColorStateList` | ` https://developer.android.com/reference/android/content/res/ColorStateList#createFromXml(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser,%20android.content.res.Resources.Theme)(https://developer.android.com/reference/android/content/res/Resources r, https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser parser, https://developer.android.com/reference/android/content/res/Resources.Theme theme) ` Creates a ColorStateList from an XML document using given a set of `https://developer.android.com/reference/android/content/res/Resources` and a `https://developer.android.com/reference/android/content/res/Resources.Theme`. |
| ` int` | ` https://developer.android.com/reference/android/content/res/ColorStateList#describeContents()() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
| ` int` | ` https://developer.android.com/reference/android/content/res/ColorStateList#getChangingConfigurations()() ` Returns a mask of the configuration parameters for which this color state list may change, requiring that it be re-created. |
| ` int` | ` https://developer.android.com/reference/android/content/res/ColorStateList#getColorForState(int[],%20int)(int[] stateSet, int defaultColor) ` Return the color associated with the given set of `https://developer.android.com/reference/android/view/View` states. |
| ` int` | ` https://developer.android.com/reference/android/content/res/ColorStateList#getDefaultColor()() ` Return the default color in this `https://developer.android.com/reference/android/content/res/ColorStateList`. |
| ` boolean` | ` https://developer.android.com/reference/android/content/res/ColorStateList#isOpaque()() ` Indicates whether this color state list is opaque, which means that every color returned from `https://developer.android.com/reference/android/content/res/ColorStateList#getColorForState(int[],%20int)` has an alpha value of 255. |
| ` boolean` | ` https://developer.android.com/reference/android/content/res/ColorStateList#isStateful()() ` Indicates whether this color state list contains at least one state spec and the first spec is not empty (e.g. match-all). |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/content/res/ColorStateList#toString()() ` Returns a string representation of the object. |
| ` static https://developer.android.com/reference/android/content/res/ColorStateList` | ` https://developer.android.com/reference/android/content/res/ColorStateList#valueOf(int)(int color) ` |
| ` https://developer.android.com/reference/android/content/res/ColorStateList` | ` https://developer.android.com/reference/android/content/res/ColorStateList#withAlpha(int)(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). |
| ` https://developer.android.com/reference/android/content/res/ColorStateList` | ` https://developer.android.com/reference/android/content/res/ColorStateList#withLStar(float)(float lStar) ` Creates a new ColorStateList that has the same states and colors as this one but where each color has the specified perceived luminosity value (0-100). |
| ` void` | ` https://developer.android.com/reference/android/content/res/ColorStateList#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) ` Flatten this object in to a Parcel. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/android/os/Parcelable ` ` abstract int` ` https://developer.android.com/reference/android/os/Parcelable#describeContents()() `

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.
` abstract void` ` https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) `

Flatten this object in to a Parcel.

## XML attributes

### android:alpha

Alpha multiplier applied to the base color.

May be a floating point value, such as "`1.2`".

<br />

### android:color

Base color for this state.

May be a color value, in the form of "`#*rgb*`", "`#*argb*`", "`#*rrggbb*`", or "`#*aarrggbb*`".

<br />

### android:lStar

Perceptual luminance applied to the base color. From 0 to 100.

May be a floating point value, such as "`1.2`".

<br />

## Fields

### CREATOR

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final Creator<ColorStateList> CREATOR
```

<br />

<br />

## Public constructors

### ColorStateList

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

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

<br />

| Parameters ||
|---|---|
| `states` | `int` <br /> |
| `colors` | `int` <br /> |

## Public methods

### createFromXml

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static ColorStateList createFromXml (Resources r, 
                XmlPullParser parser)
```

<br />

**This method was deprecated in API level 23.**   

Use #createFromXml(Resources, XmlPullParser parser, Theme)

Creates a ColorStateList from an XML document.

<br />

| Parameters ||
|---|---|
| `r` | `Resources`: Resources against which the ColorStateList should be inflated. <br /> |
| `parser` | `XmlPullParser`: Parser for the XML document defining the ColorStateList. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/res/ColorStateList` | A new color state list. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |
| `https://developer.android.com/reference/org/xmlpull/v1/XmlPullParserException` |   |

### createFromXml

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

Creates a ColorStateList from an XML document using given a set of `https://developer.android.com/reference/android/content/res/Resources` and a `https://developer.android.com/reference/android/content/res/Resources.Theme`.

<br />

| Parameters ||
|---|---|
| `r` | `Resources`: Resources against which the ColorStateList should be inflated. This value cannot be `null`. <br /> |
| `parser` | `XmlPullParser`: Parser for the XML document defining the ColorStateList. This value cannot be `null`. <br /> |
| `theme` | `Resources.Theme`: Optional theme to apply to the color state list, may be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/res/ColorStateList` | A new color state list. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |
| `https://developer.android.com/reference/org/xmlpull/v1/XmlPullParserException` |   |

### describeContents

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int describeContents ()
```

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`, the return value of this method must include the `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` bit.

<br />

| Returns ||
|---|---|
| `int` | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either `0` or - `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` |

### getChangingConfigurations

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getChangingConfigurations ()
```

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

<br />

| Returns ||
|---|---|
| `int` | a mask of the changing configuration parameters, as defined by `https://developer.android.com/reference/android/content/pm/ActivityInfo` Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_MCC` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_MNC` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_LOCALE` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_TOUCHSCREEN` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_KEYBOARD` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_KEYBOARD_HIDDEN` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_NAVIGATION` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_ORIENTATION` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_SCREEN_LAYOUT` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_UI_MODE` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_SCREEN_SIZE` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_DENSITY` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_LAYOUT_DIRECTION` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_COLOR_MODE` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_FONT_SCALE` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_GRAMMATICAL_GENDER` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_FONT_WEIGHT_ADJUSTMENT` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_ASSETS_PATHS` - `https://developer.android.com/reference/android/content/pm/ActivityInfo#CONFIG_RESOURCES_UNUSED` |

**See also:**

- `https://developer.android.com/reference/android/content/pm/ActivityInfo`

### getColorForState

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

Return the color associated with the given set of `https://developer.android.com/reference/android/view/View` states.

<br />

| Parameters ||
|---|---|
| `stateSet` | `int`: an array of `https://developer.android.com/reference/android/view/View` states. This value may be `null`. <br /> |
| `defaultColor` | `int`: the color to return if there's no matching state spec in this `https://developer.android.com/reference/android/content/res/ColorStateList` that matches the stateSet. <br /> |

| Returns ||
|---|---|
| `int` | the color associated with that set of states in this `https://developer.android.com/reference/android/content/res/ColorStateList`. <br /> |

### getDefaultColor

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getDefaultColor ()
```

Return the default color in this `https://developer.android.com/reference/android/content/res/ColorStateList`.

<br />

| Returns ||
|---|---|
| `int` | the default color in this `https://developer.android.com/reference/android/content/res/ColorStateList`. <br /> |

### isOpaque

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isOpaque ()
```

Indicates whether this color state list is opaque, which means that every color returned from `https://developer.android.com/reference/android/content/res/ColorStateList#getColorForState(int[],%20int)` has an alpha value of 255.

<br />

| Returns ||
|---|---|
| `boolean` | True if this color state list is opaque. <br /> |

### isStateful

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isStateful ()
```

Indicates whether this color state list contains at least one state spec and the first spec is not empty (e.g. match-all).

<br />

| Returns ||
|---|---|
| `boolean` | True if this color state list changes color based on state, false otherwise. <br /> |

**See also:**

- `https://developer.android.com/reference/android/content/res/ColorStateList#getColorForState(int[],%20int)`

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |

### valueOf

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static ColorStateList valueOf (int color)
```

<br />

<br />

| Parameters ||
|---|---|
| `color` | `int` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/res/ColorStateList` | A ColorStateList containing a single color. This value cannot be `null`. <br /> |

### withAlpha

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `alpha` | `int`: The new alpha channel value (0-255). <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/res/ColorStateList` | A new color state list. This value cannot be `null`. <br /> |

### withLStar

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ColorStateList withLStar (float lStar)
```

Creates a new ColorStateList that has the same states and colors as this one but where each color has the specified perceived luminosity value (0-100).

<br />

| Parameters ||
|---|---|
| `lStar` | `float`: Target perceptual luminance (0-100). <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/res/ColorStateList` | A new color state list. This value cannot be `null`. <br /> |

### writeToParcel

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void writeToParcel (Parcel dest, 
                int flags)
```

Flatten this object in to a Parcel.

<br />

| Parameters ||
|---|---|
| `dest` | `Parcel`: The Parcel in which the object should be written. This value cannot be `null`. <br /> |
| `flags` | `int`: Additional flags about how the object should be written. May be 0 or `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` |