Class CompoundButton

All Implemented Interfaces:
Drawable.Callback, ViewTreeObserver.OnPreDrawListener, Checkable
Direct Known Subclasses:
CheckBox, RadioButton, SwitchButton

public abstract class CompoundButton extends Button implements Checkable
A button with two states, checked and unchecked. When the button is pressed or clicked to perform an action, the state changes automatically.

Note that all events are handled on UI thread, this means your listener code must execute quickly to avoid delaying your UI response to further actions.

  • Field Details

    • CHECKED_STATE_SET

      public static final int[] CHECKED_STATE_SET
  • Constructor Details

    • CompoundButton

      public CompoundButton(Context context)
  • Method Details

    • toggle

      public void toggle()
      Description copied from interface: Checkable
      Change the checked state of the view to the inverse of its current state
      Specified by:
      toggle in interface Checkable
    • performClick

      public boolean performClick()
      Description copied from class: View
      Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
      Overrides:
      performClick in class View
      Returns:
      True there was an assigned OnClickListener that was called, false otherwise is returned.
    • isChecked

      public final boolean isChecked()
      Description copied from interface: Checkable
      Get the current checked state of the view
      Specified by:
      isChecked in interface Checkable
      Returns:
      The current checked state of the view
    • setChecked

      public void setChecked(boolean checked)
      Description copied from interface: Checkable
      Change the checked state of the view
      Specified by:
      setChecked in interface Checkable
      Parameters:
      checked - The new checked state
    • setOnCheckedChangeListener

      public void setOnCheckedChangeListener(@Nullable Checkable.OnCheckedChangeListener listener)
      Register a callback to be invoked when the checked state of this button changes.
      Parameters:
      listener - the callback to call on checked state change
    • setButtonDrawable

      public void setButtonDrawable(@Nullable Drawable drawable)
      Sets a drawable as the compound button image.
      Parameters:
      drawable - the drawable to set
    • getButtonDrawable

      @Nullable public Drawable getButtonDrawable()
      Returns:
      the drawable used as the compound button image
      See Also:
    • setButtonTintList

      public void setButtonTintList(@Nullable ColorStateList tint)
      Applies a tint to the button drawable. Does not modify the current tint mode, which is SRC_IN by default.

      Subsequent calls to setButtonDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable.setTintList(ColorStateList).

      Parameters:
      tint - the tint to apply, may be null to clear tint
      See Also:
    • getButtonTintList

      @Nullable public final ColorStateList getButtonTintList()
      Returns:
      the tint applied to the button drawable
      See Also:
    • getCompoundPaddingLeft

      public int getCompoundPaddingLeft()
      Description copied from class: TextView
      Returns the left padding of the view, plus space for the left Drawable if any.
      Overrides:
      getCompoundPaddingLeft in class TextView
    • getCompoundPaddingRight

      public int getCompoundPaddingRight()
      Description copied from class: TextView
      Returns the right padding of the view, plus space for the right Drawable if any.
      Overrides:
      getCompoundPaddingRight in class TextView
    • getHorizontalOffsetForDrawables

      public int getHorizontalOffsetForDrawables()
      Overrides:
      getHorizontalOffsetForDrawables in class TextView
    • onDraw

      protected void onDraw(@Nonnull Canvas canvas)
      Description copied from class: View
      Draw the content of this view, implement this to do your drawing.

      Note that (0, 0) will be the top left of the bounds, and (width, height) will be the bottom right of the bounds.

      Overrides:
      onDraw in class TextView
      Parameters:
      canvas - the canvas to draw content
    • onCreateDrawableState

      @Nonnull protected int[] onCreateDrawableState(int extraSpace)
      Description copied from class: View
      Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use View.getDrawableState().
      Overrides:
      onCreateDrawableState in class TextView
      Parameters:
      extraSpace - if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.
      Returns:
      an array holding the current Drawable state of the view.
      See Also:
    • drawableStateChanged

      protected void drawableStateChanged()
      Description copied from class: View
      This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

      If the View has a StateListAnimator, it will also be called to run necessary state change animations.

      Be sure to call through to the superclass when overriding this function.

      Overrides:
      drawableStateChanged in class TextView
      See Also:
    • drawableHotspotChanged

      public void drawableHotspotChanged(float x, float y)
      Description copied from class: View
      This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

      Dispatching to child views is handled by View.dispatchDrawableHotspotChanged(float, float).

      Be sure to call through to the superclass when overriding this function.

      Overrides:
      drawableHotspotChanged in class View
      Parameters:
      x - hotspot x coordinate
      y - hotspot y coordinate
    • onResolveDrawables

      public void onResolveDrawables(int layoutDirection)
      Description copied from class: View
      Called when layout direction has been resolved.

      The default implementation does nothing.

      Overrides:
      onResolveDrawables in class TextView
      Parameters:
      layoutDirection - The resolved layout direction.
      See Also:
    • verifyDrawable

      protected boolean verifyDrawable(@Nonnull Drawable who)
      Description copied from class: View
      If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

      Be sure to call through to the super class when overriding this function.

      Overrides:
      verifyDrawable in class TextView
      Parameters:
      who - The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
      Returns:
      boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.
      See Also:
    • jumpDrawablesToCurrentState

      public void jumpDrawablesToCurrentState()
      Description copied from class: View
      Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

      Also calls StateListAnimator.jumpToCurrentState() if there is a StateListAnimator attached to this view.

      Overrides:
      jumpDrawablesToCurrentState in class TextView