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

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.TextView
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
Public Constructors
ToggleButton(Context context, AttributeSet attrs, int defStyle)
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 setBackgroundDrawable(Drawable d)
Set the background to a given Drawable, or remove the background.
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.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.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 defStyle)

public ToggleButton (Context context, AttributeSet attrs)

public ToggleButton (Context context)

Public Methods

public CharSequence getTextOff ()

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

Returns
  • The text.

public CharSequence getTextOn ()

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

Returns
  • The text.

public void setBackgroundDrawable (Drawable d)

Set the background to a given Drawable, or remove the background. If the background has padding, this View's padding is set to the background's padding. However, when a background is removed, this View's padding isn't touched. If setting the padding is desired, please use setPadding(int, int, int, int).

Parameters
d The Drawable to use as the background, or null to remove the background

public void setChecked (boolean checked)

Changes the checked state of this button.

Parameters
checked true to check the button, false to uncheck it

public void setTextOff (CharSequence textOff)

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

Parameters
textOff The text.

public void setTextOn (CharSequence textOn)

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

Parameters
textOn The text.

Protected Methods

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.

Be sure to call through to the superclass when overriding this function.

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.