to top
Android APIs
public abstract class

CompoundButton

extends Button
implements Checkable
java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ android.widget.CompoundButton
Known Direct Subclasses

Class Overview

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. 
[Expand]
Inherited XML Attributes
From class android.widget.TextView
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
CompoundButton(Context context)
CompoundButton(Context context, AttributeSet attrs)
CompoundButton(Context context, AttributeSet attrs, int defStyle)
Public Methods
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 onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
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)
Set the background to a given Drawable, identified by its resource id.
void setButtonDrawable(Drawable d)
Set the background to a given 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.
[Expand]
Inherited Methods
From class android.widget.Button
From class android.widget.TextView
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.view.accessibility.AccessibilityEventSource
From interface android.widget.Checkable

Public Constructors

public CompoundButton (Context context)

Added in API level 1

public CompoundButton (Context context, AttributeSet attrs)

Added in API level 1

public CompoundButton (Context context, AttributeSet attrs, int defStyle)

Added in API level 1

Public Methods

public int getCompoundPaddingLeft ()

Added in API level 1

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

public int getCompoundPaddingRight ()

Added in API level 1

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

public boolean isChecked ()

Added in API level 1

public void jumpDrawablesToCurrentState ()

Added in API level 11

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

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public void onRestoreInstanceState (Parcelable state)

Added in API level 1

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 The frozen state that had previously been returned by onSaveInstanceState().

public Parcelable onSaveInstanceState ()

Added in API level 1

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
  • Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.

public boolean performClick ()

Added in API level 1

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

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

public void setButtonDrawable (int resid)

Added in API level 1

Set the background to a given Drawable, identified by its resource id.

Parameters
resid the resource id of the drawable to use as the background

public void setButtonDrawable (Drawable d)

Added in API level 1

Set the background to a given Drawable

Parameters
d The Drawable to use as the background

public void setChecked (boolean checked)

Added in API level 1

Changes the checked state of this button.

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

public void setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)

Added in API level 1

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

Parameters
listener the callback to call on checked state change

public void toggle ()

Added in API level 1

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

Protected Methods

protected void drawableStateChanged ()

Added in API level 1

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

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

protected int[] onCreateDrawableState (int extraSpace)

Added in API level 1

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 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
  • Returns an array holding the current Drawable state of the view.

protected void onDraw (Canvas canvas)

Added in API level 1

Implement this to do your drawing.

Parameters
canvas the canvas on which the background will be drawn

protected boolean verifyDrawable (Drawable who)

Added in API level 1

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 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 than the Drawable is being displayed in the view; else false and it is not allowed to animate.