Android APIs
public class

ToggleButton

extends CompoundButton
java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ android.widget.CompoundButton
           ↳ android.widget.ToggleButton

Class Overview

Displays checked/unchecked states as a button with a "light" indicator and by default accompanied with the text "ON" or "OFF".

See the Toggle Buttons guide.

Summary

XML Attributes
Attribute Name Related Method Description
android:disabledAlpha The alpha to apply to the indicator when disabled. 
android:textOff The text for the button when it is not checked. 
android:textOn The text for the button when it is checked. 
[Expand]
Inherited XML Attributes
From class android.widget.CompoundButton
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
ToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
ToggleButton(Context context, AttributeSet attrs, int defStyleAttr)
ToggleButton(Context context, AttributeSet attrs)
ToggleButton(Context context)
Public Methods
CharSequence getTextOff()
Returns the text for when the button is not in the checked state.
CharSequence getTextOn()
Returns the text for when the button is in the checked state.
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 setBackgroundDrawable(Drawable d)
This method is deprecated. use setBackground(Drawable) instead
void setChecked(boolean checked)

Changes the checked state of this button.

void setTextOff(CharSequence textOff)
Sets the text for when the button is not in the checked state.
void setTextOn(CharSequence textOn)
Sets the text for when the button is in the checked 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.
void onFinishInflate()
Finalize inflating a view from XML.
[Expand]
Inherited Methods
From class android.widget.CompoundButton
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

XML Attributes

android:disabledAlpha

The alpha to apply to the indicator when disabled.

Must be a floating point value, such as "1.2".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol disabledAlpha.

Related Methods

android:textOff

The text for the button when it is not checked.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol textOff.

Related Methods

android:textOn

The text for the button when it is checked.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol textOn.

Related Methods

Public Constructors

public ToggleButton (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Added in API level 21

public ToggleButton (Context context, AttributeSet attrs, int defStyleAttr)

Added in API level 1

public ToggleButton (Context context, AttributeSet attrs)

Added in API level 1

public ToggleButton (Context context)

Added in API level 1

Public Methods

public CharSequence getTextOff ()

Added in API level 1

Returns the text for when the button is not in the checked state.

Returns
  • The text.

public CharSequence getTextOn ()

Added in API level 1

Returns the text for when the button is in the checked state.

Returns
  • The text.

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 setBackgroundDrawable (Drawable d)

Added in API level 1

This method is deprecated.
use setBackground(Drawable) instead

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 setTextOff (CharSequence textOff)

Added in API level 1

Sets the text for when the button is not in the checked state.

Parameters
textOff The text.

public void setTextOn (CharSequence textOn)

Added in API level 1

Sets the text for when the button is in the checked state.

Parameters
textOn The text.

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.

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.

protected void onFinishInflate ()

Added in API level 1

Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added.

Even if the subclass overrides onFinishInflate, they should always be sure to call the super method, so that we get called.