Class StateListDrawable

All Implemented Interfaces:
Drawable.Callback

public class StateListDrawable extends DrawableContainer
Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.
  • Constructor Details

    • StateListDrawable

      public StateListDrawable()
  • Method Details

    • addState

      public void addState(int[] stateSet, Drawable drawable)
      Add a new image/string ID to the set of images.
      Parameters:
      stateSet - An array of resource Ids to associate with the image. Switch to this image by calling setState().
      drawable - The image to show. Note this must be a unique Drawable that is not shared between any other View or Drawable otherwise the results are undefined and can lead to unexpected rendering behavior
    • isStateful

      public boolean isStateful()
      Description copied from class: Drawable
      Indicates whether this drawable will change its appearance based on state. Clients can use this to determine whether it is necessary to calculate their state and call setState.
      Overrides:
      isStateful in class DrawableContainer
      Returns:
      True if this drawable changes its appearance based on state, false otherwise.
      See Also:
    • hasFocusStateSpecified

      public boolean hasFocusStateSpecified()
      Description copied from class: Drawable
      Indicates whether this drawable has at least one state spec explicitly specifying state_focused.

      Note: A View uses a Drawable instance as its background and it changes its appearance based on a state. On keyboard devices, it should specify its state_focused to make sure the user knows which view is holding the focus.

      Overrides:
      hasFocusStateSpecified in class DrawableContainer
      Returns:
      true if state_focused is specified for this drawable.
    • onStateChange

      protected boolean onStateChange(@NonNull int[] stateSet)
      Description copied from class: Drawable
      Override this in your subclass to change appearance if you recognize the specified state.
      Overrides:
      onStateChange in class DrawableContainer
      Returns:
      Returns true if the state change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last state.
    • getStateCount

      public int getStateCount()
      Gets the number of states contained in this drawable.
      Returns:
      The number of states contained in this drawable.
      See Also:
    • getStateSet

      @NonNull public int[] getStateSet(int index)
      Gets the state set at an index.
      Parameters:
      index - The index of the state set.
      Returns:
      The state set at the index.
      See Also:
    • getStateDrawable

      @Nullable public Drawable getStateDrawable(int index)
      Gets the drawable at an index.
      Parameters:
      index - The index of the drawable.
      Returns:
      The drawable at the index.
      See Also:
    • findStateDrawableIndex

      public int findStateDrawableIndex(@NonNull int[] stateSet)
      Gets the index of the drawable with the provided state set.
      Parameters:
      stateSet - the state set to look up
      Returns:
      the index of the provided state set, or -1 if not found
      See Also:
    • mutate

      @NonNull public Drawable mutate()
      Description copied from class: Drawable
      Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

      Calling this method on a mutable Drawable will have no effect.

      Overrides:
      mutate in class DrawableContainer
      Returns:
      This drawable.
      See Also:
    • clearMutated

      public void clearMutated()
      Description copied from class: Drawable
      Clears the mutated state, allowing this drawable to be cached and mutated again.
      Overrides:
      clearMutated in class DrawableContainer
    • setConstantState

      protected void setConstantState(@NonNull DrawableContainer.DrawableContainerState state)
      Overrides:
      setConstantState in class DrawableContainer