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 getAccessibilityClassName()
Return the class name of this object to be used for accessibility purposes.
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 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.widget.Checkable
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

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 getAccessibilityClassName ()

Added in API level 23

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.

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