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.

float getDisabledAlpha()

Returns the alpha value of the button when it is disabled

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(android.graphics.drawable.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 1
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

getDisabledAlpha

Added in API level 29
public float getDisabledAlpha ()

Returns the alpha value of the button when it is disabled

Returns
float the alpha value, 0.0-1.0 Value is between 0.0 and 1.0 inclusive

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(android.graphics.drawable.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.
If you override this method you must call through to the superclass implementation.

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.
If you override this method you must call through to the superclass implementation.