CompoundButton

public abstract class CompoundButton
extends Button implements Checkable

java.lang.Object
   ↳ 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 Attributes, TextView Attributes, View Attributes

Summary

Nested classes

interface CompoundButton.OnCheckedChangeListener

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

XML attributes

android:button Drawable used for the button graphic (for example, checkbox and radio button). 
android:buttonTint Tint to apply to the button graphic. 
android:buttonTintMode Blending mode used to apply the button graphic tint. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

CompoundButton(Context context)
CompoundButton(Context context, AttributeSet attrs)
CompoundButton(Context context, AttributeSet attrs, int defStyleAttr)
CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void autofill(AutofillValue value)

Automatically fills the content of this view with the value.

void drawableHotspotChanged(float x, float y)

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

CharSequence getAccessibilityClassName()

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

int getAutofillType()

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

AutofillValue getAutofillValue()

Gets the TextView's current text for AutoFill.

Drawable getButtonDrawable()
ColorStateList getButtonTintList()
PorterDuff.Mode getButtonTintMode()
int getCompoundPaddingLeft()

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

int getCompoundPaddingRight()

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

boolean isChecked()
void jumpDrawablesToCurrentState()

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

void onProvideAutofillStructure(ViewStructure structure, int flags)

Populates a ViewStructure to fullfil an autofill request.

void onRestoreInstanceState(Parcelable state)

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

Parcelable onSaveInstanceState()

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.

boolean performClick()

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

void setButtonDrawable(int resId)

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

void setButtonDrawable(Drawable drawable)

Sets a drawable as the compound button image.

void setButtonTintList(ColorStateList tint)

Applies a tint to the button drawable.

void setButtonTintMode(PorterDuff.Mode tintMode)

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

void setChecked(boolean checked)

Changes the checked state of this button.

void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)

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

void toggle()

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

Protected methods

void drawableStateChanged()

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

int[] onCreateDrawableState(int extraSpace)

Generate the new Drawable state for this view.

void onDraw(Canvas canvas)

Implement this to do your drawing.

boolean verifyDrawable(Drawable who)

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 methods

XML attributes

android:button

Drawable used for the button graphic (for example, checkbox and radio button).

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Related methods:

android:buttonTint

Tint to apply to the button graphic.

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

Related methods:

android:buttonTintMode

Blending mode used to apply the button graphic tint.

Must be one of the following constant values.

ConstantValueDescription
add10Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D)
multiplyeMultiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]
screenf[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
src_atop9The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
src_in5The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]
src_over3The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]

Related methods:

Public constructors

CompoundButton

added in API level 1
public CompoundButton (Context context)

Parameters
context Context

CompoundButton

added in API level 1
public CompoundButton (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

CompoundButton

added in API level 1
public CompoundButton (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

CompoundButton

added in API level 21
public CompoundButton (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

autofill

added in API level 26
public void autofill (AutofillValue value)

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(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
public void drawableHotspotChanged (float x, 
                float y)

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.

Parameters
x float: hotspot x coordinate

y float: hotspot y coordinate

getAccessibilityClassName

added in API level 23
public CharSequence getAccessibilityClassName ()

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.

Returns
CharSequence

getAutofillType

added in API level 26
public int getAutofillType ()

Describes the autofill type of this view, so an 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.

Returns
int either AUTOFILL_TYPE_NONE, AUTOFILL_TYPE_TEXT, AUTOFILL_TYPE_LIST, AUTOFILL_TYPE_DATE, or AUTOFILL_TYPE_TOGGLE.

Value is AUTOFILL_TYPE_NONE, AUTOFILL_TYPE_TEXT, AUTOFILL_TYPE_TOGGLE, AUTOFILL_TYPE_LIST or AUTOFILL_TYPE_DATE.

getAutofillValue

added in API level 26
public AutofillValue getAutofillValue ()

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

Returns
AutofillValue current text, null if the text is not editable

getButtonDrawable

added in API level 23
public Drawable getButtonDrawable ()

Returns
Drawable the drawable used as the compound button image

This value may be null.

getButtonTintList

added in API level 21
public ColorStateList getButtonTintList ()

Related XML Attributes:

Returns
ColorStateList the tint applied to the button drawable

This value may be null.

getButtonTintMode

added in API level 21
public PorterDuff.Mode getButtonTintMode ()

Related XML Attributes:

Returns
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
public int getCompoundPaddingLeft ()

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

Returns
int

getCompoundPaddingRight

added in API level 1
public int getCompoundPaddingRight ()

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

Returns
int

isChecked

added in API level 1
public boolean isChecked ()

Returns
boolean

jumpDrawablesToCurrentState

added in API level 11
public void jumpDrawablesToCurrentState ()

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

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

onProvideAutofillStructure

added in API level 26
public void onProvideAutofillStructure (ViewStructure structure, 
                int flags)

Populates a ViewStructure to fullfil an autofill request.

The structure should contain at least the following properties:

It's also recommended to set the following properties - the more properties the structure has, the higher the changes of an AutofillService properly using the structure:

The default implementation of this method already sets most of these properties based on related View methods (for example, the autofill id is set using getAutofillId(), the autofill type set using getAutofillType(), etc.), and views in the standard Android widgets library also override it to set their relevant properties (for example, TextView already sets the text properties), so it's recommended to only override this method (and call super.onProvideAutofillStructure()) when:

Note: The left and top values set in ViewStructure.setDimens(int, int, int, int, int, int) must be relative to the next isImportantForAutofill() predecessor view included in the structure.

Views support the Autofill Framework mainly by:

  • Providing the metadata defining what the view means and how it can be autofilled.
  • Notifying the Android System when the view value changed by calling AutofillManager.notifyValueChanged(View).
  • Implementing the methods that autofill the view.

This method is responsible for the former; autofill(AutofillValue) is responsible for the latter.

Parameters
structure ViewStructure: fill in with structured view data for autofill purposes.

flags int: optional flags.

onRestoreInstanceState

added in API level 1
public void onRestoreInstanceState (Parcelable state)

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.

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

onSaveInstanceState

added in API level 1
public Parcelable onSaveInstanceState ()

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.

Returns
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
public boolean performClick ()

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

Returns
boolean True there was an assigned OnClickListener that was called, false otherwise is returned.

setButtonDrawable

added in API level 1
public void setButtonDrawable (int resId)

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

Related XML Attributes:

Parameters
resId int: the resource identifier of the drawable

setButtonDrawable

added in API level 1
public void setButtonDrawable (Drawable drawable)

Sets a drawable as the compound button image.

Related XML Attributes:

Parameters
drawable Drawable: the drawable to set

This value may be null.

setButtonTintList

added in API level 21
public void setButtonTintList (ColorStateList tint)

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(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable.setTintList(ColorStateList).

Related XML Attributes:

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

setButtonTintMode

added in API level 21
public void setButtonTintMode (PorterDuff.Mode tintMode)

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

Related XML Attributes:

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

setChecked

added in API level 1
public void setChecked (boolean checked)

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
public void setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)

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.

toggle

added in API level 1
public void toggle ()

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

Protected methods

drawableStateChanged

added in API level 1
protected void drawableStateChanged ()

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.

onCreateDrawableState

added in API level 1
protected int[] onCreateDrawableState (int extraSpace)

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 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.

Returns
int[] Returns an array holding the current Drawable state of the view.

onDraw

added in API level 1
protected void onDraw (Canvas canvas)

Implement this to do your drawing.

Parameters
canvas Canvas: the canvas on which the background will be drawn

verifyDrawable

added in API level 1
protected boolean verifyDrawable (Drawable who)

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.

Parameters
who Drawable: The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.

This value must never be null.

Returns
boolean boolean If true than the Drawable is being displayed in the view; else false and it is not allowed to animate.