Added in API level 1

ToggleButton


open class ToggleButton : CompoundButton
kotlin.Any
   ↳ 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
Public constructors
ToggleButton(context: Context!)

ToggleButton(context: Context!, attrs: AttributeSet!)

ToggleButton(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

ToggleButton(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open CharSequence!

Return the class name of this object to be used for accessibility purposes.

open Float

Returns the alpha value of the button when it is disabled

open CharSequence!

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

open CharSequence!

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

open Unit

open Unit
setChecked(checked: Boolean)

Changes the checked state of this button.

open Unit

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

open Unit

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

Protected methods
open Unit

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

open Unit

Finalize inflating a view from XML.

Inherited functions
Inherited properties

XML attributes

android:disabledAlpha

android:disabledAlpha
The alpha to apply to the indicator when disabled.

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

android:textOff

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

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
ToggleButton(context: Context!)

ToggleButton

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

ToggleButton

Added in API level 1
ToggleButton(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

ToggleButton

Added in API level 21
ToggleButton(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

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.

getDisabledAlpha

Added in API level 29
open fun getDisabledAlpha(): Float

Returns the alpha value of the button when it is disabled

Return
Float the alpha value, 0.0-1.0.
Value is between 0.0f and 1.0f inclusive

getTextOff

Added in API level 1
open fun getTextOff(): CharSequence!

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

Return
CharSequence! The text.

getTextOn

Added in API level 1
open fun getTextOn(): CharSequence!

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

Return
CharSequence! The text.

setBackgroundDrawable

Added in API level 1
open fun setBackgroundDrawable(d: Drawable!): Unit

setChecked

Added in API level 1
open fun setChecked(checked: Boolean): Unit

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
open fun setTextOff(textOff: CharSequence!): Unit

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

Parameters
textOff CharSequence!: The text.

setTextOn

Added in API level 1
open fun setTextOn(textOn: CharSequence!): Unit

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 open fun drawableStateChanged(): Unit

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 open fun onFinishInflate(): Unit

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.