Skip to content

Most visited

Recently visited

navigation

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

From class android.widget.CompoundButton
From class android.widget.TextView
From class android.view.View

Inherited constants

From class android.view.View

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.

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.

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.

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.

Public constructors

ToggleButton

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

Parameters
context Context
attrs AttributeSet
defStyleAttr int
defStyleRes int

ToggleButton

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

Parameters
context Context
attrs AttributeSet
defStyleAttr int

ToggleButton

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

Parameters
context Context
attrs AttributeSet

ToggleButton

Added in API level 1
ToggleButton (Context context)

Parameters
context Context

Public methods

getAccessibilityClassName

Added in API level 23
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
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
CharSequence getTextOn ()

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

Returns
CharSequence The text.

setBackgroundDrawable

Added in API level 1
void setBackgroundDrawable (Drawable d)

This method is deprecated.
use setBackground(Drawable) instead

Parameters
d Drawable

setChecked

Added in API level 1
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
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
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
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
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.

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.