Added in API level 1

CompoundButton


abstract class CompoundButton : Button, Checkable
kotlin.Any
   ↳ android.view.View
   ↳ android.widget.TextView
   ↳ android.widget.Button
   ↳ android.widget.CompoundButton

A button with two states, checked and unchecked. When the button is pressed or clicked, the state changes automatically.

XML attributes

See CompoundButton Attributes, Button, TextView Attributes, View Attributes

Summary

Nested classes
abstract

Interface definition for a callback to be invoked when the checked state of a compound button changed.

Inherited XML attributes
Inherited constants
Public constructors

CompoundButton(context: Context!, attrs: AttributeSet!)

CompoundButton(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

CompoundButton(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Unit

Automatically fills the content of this view with the value.

open Unit

This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

open CharSequence!

Return the class name of this object to be used for accessibility purposes.

open Int

Describes the autofill type of this view, so an android.service.autofill.AutofillService can create the proper AutofillValue when autofilling the view.

open AutofillValue?

Gets the TextView's current text for AutoFill.

open Drawable?

open BlendMode?

open ColorStateList?

open PorterDuff.Mode?

open Int

Returns the left padding of the view, plus space for the left Drawable if any.

open Int

Returns the right padding of the view, plus space for the right Drawable if any.

open Boolean

open Unit

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

open Unit

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState.

open Parcelable?

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.

open Boolean

Call this view's OnClickListener, if it is defined.

open Unit

Sets a drawable as the compound button image.

open Unit

Sets a drawable as the compound button image given its resource identifier.

open Unit

Sets the button of this CompoundButton to the specified Icon.

open Unit

Specifies the blending mode used to apply the tint specified by setButtonTintList(android.content.res.ColorStateList)} to the button drawable.

open Unit

Applies a tint to the button drawable.

open Unit

Specifies the blending mode used to apply the tint specified by setButtonTintList(android.content.res.ColorStateList)} to the button drawable.

open Unit
setChecked(checked: Boolean)

Changes the checked state of this button.

open Unit

Register a callback to be invoked when the checked state of this button changes.

open Unit
setStateDescription(stateDescription: CharSequence?)

This function is called when an instance or subclass sets the state description.

open Unit

Change the checked state of the view to the inverse of its current state

Protected methods
open Unit

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

open IntArray!

Generate the new android.graphics.drawable.Drawable state for this view.

open Unit
onDraw(canvas: Canvas)

Implement this to do your drawing.

open Boolean

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.

Inherited functions
Inherited properties

Public constructors

CompoundButton

Added in API level 1
CompoundButton(context: Context!)

CompoundButton

Added in API level 1
CompoundButton(
    context: Context!,
    attrs: AttributeSet!)

CompoundButton

Added in API level 1
CompoundButton(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

CompoundButton

Added in API level 21
CompoundButton(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

autofill

Added in API level 26
open fun autofill(value: AutofillValue!): Unit

Automatically fills the content of this view with the value.

Views support the Autofill Framework mainly by:

  • Providing the metadata defining what the view means and how it can be autofilled.
  • Implementing the methods that autofill the view.

onProvideAutofillStructure(android.view.ViewStructure,int) is responsible for the former, this method is responsible for latter.

This method does nothing by default, but when overridden it typically:

  1. Checks if the provided value matches the expected type (which is defined by getAutofillType()).
  2. Checks if the view is editable - if it isn't, it should return right away.
  3. Call the proper getter method on AutofillValue to fetch the actual value.
  4. Pass the actual value to the equivalent setter in the view.

For example, a text-field view could implement the method this way:

@Override
  public void autofill(AutofillValue value) {
    if (!value.isText() || !this.isEditable()) {
       return;
    }
    CharSequence text = value.getTextValue();
    if (text != null) {
      this.setText(text);
    }
  }
  

If the value is updated asynchronously, the next call to AutofillManager.notifyValueChanged(View) must happen after the value was changed to the autofilled value. If not, the view will not be considered autofilled.

Note: After this method is called, the value returned by getAutofillValue() must be equal to the value passed to it, otherwise the view will not be highlighted as autofilled.

Parameters
value AutofillValue!: value to be autofilled.

drawableHotspotChanged

Added in API level 21
open fun drawableHotspotChanged(
    x: Float,
    y: Float
): Unit

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 dispatchDrawableHotspotChanged(float,float).

Be sure to call through to the superclass when overriding this function.
If you override this method you must call through to the superclass implementation.

Parameters
x Float: hotspot x coordinate
y Float: hotspot y coordinate

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

getAutofillType

Added in API level 26
open fun getAutofillType(): Int

Describes the autofill type of this view, so an android.service.autofill.AutofillService can create the proper AutofillValue when autofilling the view.

By default returns AUTOFILL_TYPE_NONE, but views should override it to properly support the Autofill Framework.

Return
Int Value is one of the following:

getAutofillValue

Added in API level 26
open fun getAutofillValue(): AutofillValue?

Gets the TextView's current text for AutoFill. The value is trimmed to 100K chars if longer.

Return
AutofillValue? current text, null if the text is not editable

getButtonDrawable

Added in API level 23
open fun getButtonDrawable(): Drawable?
Return
Drawable? the drawable used as the compound button image.
This value may be null.

getButtonTintBlendMode

Added in API level 29
open fun getButtonTintBlendMode(): BlendMode?
Return
BlendMode? the blending mode used to apply the tint to the button drawable.
This value may be null.

getButtonTintList

Added in API level 21
open fun getButtonTintList(): ColorStateList?
Return
ColorStateList? the tint applied to the button drawable.
This value may be null.

getButtonTintMode

Added in API level 21
open fun getButtonTintMode(): PorterDuff.Mode?
Return
PorterDuff.Mode? the blending mode used to apply the tint to the button drawable.
This value may be null.

getCompoundPaddingLeft

Added in API level 1
open fun getCompoundPaddingLeft(): Int

Returns the left padding of the view, plus space for the left Drawable if any.

getCompoundPaddingRight

Added in API level 1
open fun getCompoundPaddingRight(): Int

Returns the right padding of the view, plus space for the right Drawable if any.

isChecked

Added in API level 1
open fun isChecked(): Boolean
Return
Boolean The current checked state of the view

jumpDrawablesToCurrentState

Added in API level 11
open fun jumpDrawablesToCurrentState(): Unit

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

Also calls StateListAnimator.jumpToCurrentState() if there is a StateListAnimator attached to this view.
If you override this method you must call through to the superclass implementation.

onRestoreInstanceState

Added in API level 1
open fun onRestoreInstanceState(state: Parcelable!): Unit

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState. This function will never be called with a null state.
If you override this method you must call through to the superclass implementation.

Parameters
state Parcelable!: The frozen state that had previously been returned by onSaveInstanceState.

onSaveInstanceState

Added in API level 1
open fun onSaveInstanceState(): Parcelable?

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.
If you override this method you must call through to the superclass implementation.

Return
Parcelable? Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save.

performClick

Added in API level 1
open fun performClick(): Boolean

Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.

Return
Boolean True there was an assigned OnClickListener that was called, false otherwise is returned.

setButtonDrawable

Added in API level 1
open fun setButtonDrawable(drawable: Drawable?): Unit

Sets a drawable as the compound button image.

Parameters
drawable Drawable?: the drawable to set.
This value may be null.

setButtonDrawable

Added in API level 1
open fun setButtonDrawable(resId: Int): Unit

Sets a drawable as the compound button image given its resource identifier.

Parameters
resId Int: the resource identifier of the drawable

setButtonIcon

Added in API level 31
open fun setButtonIcon(icon: Icon?): Unit

Sets the button of this CompoundButton to the specified Icon.

Parameters
icon Icon?: an Icon holding the desired button, or null to clear the button

setButtonTintBlendMode

Added in API level 29
open fun setButtonTintBlendMode(tintMode: BlendMode?): Unit

Specifies the blending mode used to apply the tint specified by setButtonTintList(android.content.res.ColorStateList)} to the button drawable. The default mode is PorterDuff.Mode.SRC_IN.

Parameters
tintMode BlendMode?: the blending mode used to apply the tint, may be null to clear tint

setButtonTintList

Added in API level 21
open fun setButtonTintList(tint: ColorStateList?): Unit

Applies a tint to the button drawable. Does not modify the current tint mode, which is PorterDuff.Mode.SRC_IN by default.

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

Parameters
tint ColorStateList?: the tint to apply, may be null to clear tint

setButtonTintMode

Added in API level 21
open fun setButtonTintMode(tintMode: PorterDuff.Mode?): Unit

Specifies the blending mode used to apply the tint specified by setButtonTintList(android.content.res.ColorStateList)} to the button drawable. The default mode is PorterDuff.Mode.SRC_IN.

Parameters
tintMode PorterDuff.Mode?: the blending mode used to apply the tint, may be null to clear tint

setChecked

Added in API level 1
open fun setChecked(checked: Boolean): Unit

Changes the checked state of this button.

Parameters
checked Boolean: true to check the button, false to uncheck it

setOnCheckedChangeListener

Added in API level 1
open fun setOnCheckedChangeListener(listener: CompoundButton.OnCheckedChangeListener?): Unit

Register a callback to be invoked when the checked state of this button changes.

Parameters
listener CompoundButton.OnCheckedChangeListener?: the callback to call on checked state change.
This value may be null.

setStateDescription

Added in API level 30
open fun setStateDescription(stateDescription: CharSequence?): Unit

This function is called when an instance or subclass sets the state description. Once this is called and the argument is not null, the app developer will be responsible for updating state description when checked state changes and we will not set state description in setChecked. App developers can restore the default behavior by setting the argument to null. If setChecked is called first and then setStateDescription is called, two state change events will be merged by event throttling and we can still get the correct state description.

Parameters
stateDescription CharSequence?: The state description.
This value may be null.

toggle

Added in API level 1
open fun toggle(): Unit

Change the checked state of the view to the inverse of its current state

Protected methods

drawableStateChanged

Added in API level 1
protected open fun drawableStateChanged(): Unit

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.
If you override this method you must call through to the superclass implementation.

onCreateDrawableState

Added in API level 1
protected open fun onCreateDrawableState(extraSpace: Int): IntArray!

Generate the new android.graphics.drawable.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 getDrawableState.

Parameters
extraSpace Int: 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.
Return
IntArray! Returns an array holding the current Drawable state of the view.

onDraw

Added in API level 1
protected open fun onDraw(canvas: Canvas): Unit

Implement this to do your drawing.

Parameters
canvas Canvas: the canvas on which the background will be drawn.
This value cannot be null.

verifyDrawable

Added in API level 1
protected open fun verifyDrawable(who: Drawable): Boolean

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.
If you override this method you must call through to the superclass implementation.

Parameters
who Drawable: This value cannot be null.
Return
Boolean boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.