Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

ToggleButton

public class ToggleButton
extends CompoundButton

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


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

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. 

Inherited XML attributes

Inherited constants

Inherited fields

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.

Inherited methods

XML attributes

android:disabledAlpha

The alpha to apply to the indicator when disabled.

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

android:textOff

The text for the button when it is not checked.

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

android:textOn

The text for the button when it is checked.

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

Public constructors

ToggleButton

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

ToggleButton

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

ToggleButton

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

Parameters
context Context

attrs AttributeSet

ToggleButton

added in API level 1
public ToggleButton (Context context)

Parameters
context Context

Public methods

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

getTextOff

added in API level 1
public CharSequence getTextOff ()

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

Returns
CharSequence The text.

getTextOn

added in API level 1
public CharSequence getTextOn ()

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

Returns
CharSequence The text.

setBackgroundDrawable

added in API level 1
public void setBackgroundDrawable (Drawable d)

This method is deprecated.
use setBackground(Drawable) instead

Parameters
d Drawable

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

setTextOff

added in API level 1
public void setTextOff (CharSequence textOff)

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

Parameters
textOff CharSequence: The text.

setTextOn

added in API level 1
public void setTextOn (CharSequence textOn)

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

Parameters
textOn CharSequence: The text.

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.

onFinishInflate

added in API level 1
protected void onFinishInflate ()

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.