Class ColorStateList

java.lang.Object
icyllis.modernui.util.ColorStateList

public class ColorStateList extends Object
Lets you map View state sets to colors.

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.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ColorStateList(int[][] states, int[] colors)
    Creates a ColorStateList that returns the specified mapping from states to colors.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    getColorForState(int[] stateSet, int defaultColor)
    Return the color associated with the given set of View states.
    int
    Return the default color in this ColorStateList.
    boolean
    Return whether the state spec list has at least one item explicitly specifying
    boolean
    hasState(int state)
    Returns whether the specified state is referenced in any of the state specs contained within this ColorStateList.
    boolean
    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
    Indicates whether this color state list contains at least one state spec and the first spec is not empty (e.g.
    static int
    modulateColor(int baseColor, float alphaMod)
    Apply alpha modulation to base color.
     
    valueOf(int color)
     
    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).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ColorStateList

      public ColorStateList(@NonNull int[][] states, @NonNull int[] colors)
      Creates a ColorStateList that returns the specified mapping from states to colors. The given arrays should not be modified after construction.
  • Method Details

    • valueOf

      @NonNull public static ColorStateList valueOf(@ColorInt int color)
      Returns:
      A ColorStateList containing a single color.
    • withAlpha

      @NonNull 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).
      Parameters:
      alpha - The new alpha channel value (0-255).
      Returns:
      A new color state list.
    • modulateColor

      public static int modulateColor(int baseColor, float alphaMod)
      Apply alpha modulation to base color.
      Parameters:
      baseColor - base color
      alphaMod - alpha multiplier applied to the base color
      Returns:
      modulated color
    • isStateful

      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).
      Returns:
      True if this color state list changes color based on state, false otherwise.
      See Also:
    • hasFocusStateSpecified

      @Internal public boolean hasFocusStateSpecified()
      Return whether the state spec list has at least one item explicitly specifying
    • isOpaque

      public 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.
      Returns:
      True if this color state list is opaque.
    • getColorForState

      public int getColorForState(int[] stateSet, int defaultColor)
      Return the color associated with the given set of View states.
      Parameters:
      stateSet - an array of View states
      defaultColor - the color to return if there's no matching state spec in this ColorStateList that matches the stateSet.
      Returns:
      the color associated with that set of states in this ColorStateList.
    • getDefaultColor

      @ColorInt public int getDefaultColor()
      Return the default color in this ColorStateList.
      Returns:
      the default color in this ColorStateList.
    • hasState

      @Internal public boolean hasState(int state)
      Returns whether the specified state is referenced in any of the state specs contained within this ColorStateList.

      Any reference, either positive or negative {ex. ~R.attr.state_enabled}, will cause this method to return true. Wildcards are not counted as references.

      Parameters:
      state - the state to search for
      Returns:
      true if the state if referenced, false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object