FileDocCategorySizeDatePackage
ToggleButton.javaAPI DocAndroid 5.1 API5245Thu Mar 12 22:22:10 GMT 2015android.widget

ToggleButton

public class ToggleButton extends CompoundButton
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.

attr
ref android.R.styleable#ToggleButton_textOn
attr
ref android.R.styleable#ToggleButton_textOff
attr
ref android.R.styleable#ToggleButton_disabledAlpha

Fields Summary
private CharSequence
mTextOn
private CharSequence
mTextOff
private android.graphics.drawable.Drawable
mIndicatorDrawable
private static final int
NO_ALPHA
private float
mDisabledAlpha
Constructors Summary
public ToggleButton(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)


             
        super(context, attrs, defStyleAttr, defStyleRes);

        final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.ToggleButton, defStyleAttr, defStyleRes);
        mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn);
        mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff);
        mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f);
        syncTextState();
        a.recycle();
    
public ToggleButton(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        this(context, attrs, defStyleAttr, 0);
    
public ToggleButton(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
    
public ToggleButton(android.content.Context context)

        this(context, null);
    
Methods Summary
protected voiddrawableStateChanged()

        super.drawableStateChanged();
        
        if (mIndicatorDrawable != null) {
            mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
        }
    
public java.lang.CharSequencegetTextOff()
Returns the text for when the button is not in the checked state.

return
The text.

        return mTextOff;
    
public java.lang.CharSequencegetTextOn()
Returns the text for when the button is in the checked state.

return
The text.

        return mTextOn;
    
protected voidonFinishInflate()

        super.onFinishInflate();
        
        updateReferenceToIndicatorDrawable(getBackground());
    
public voidonInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)

        super.onInitializeAccessibilityEvent(event);
        event.setClassName(ToggleButton.class.getName());
    
public voidonInitializeAccessibilityNodeInfo(android.view.accessibility.AccessibilityNodeInfo info)

        super.onInitializeAccessibilityNodeInfo(info);
        info.setClassName(ToggleButton.class.getName());
    
public voidsetBackgroundDrawable(android.graphics.drawable.Drawable d)

        super.setBackgroundDrawable(d);
        
        updateReferenceToIndicatorDrawable(d);
    
public voidsetChecked(boolean checked)

        super.setChecked(checked);
        
        syncTextState();
    
public voidsetTextOff(java.lang.CharSequence textOff)
Sets the text for when the button is not in the checked state.

param
textOff The text.

        mTextOff = textOff;
    
public voidsetTextOn(java.lang.CharSequence textOn)
Sets the text for when the button is in the checked state.

param
textOn The text.

        mTextOn = textOn;
    
private voidsyncTextState()

        boolean checked = isChecked();
        if (checked && mTextOn != null) {
            setText(mTextOn);
        } else if (!checked && mTextOff != null) {
            setText(mTextOff);
        }
    
private voidupdateReferenceToIndicatorDrawable(android.graphics.drawable.Drawable backgroundDrawable)

        if (backgroundDrawable instanceof LayerDrawable) {
            LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable;
            mIndicatorDrawable =
                    layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle);
        } else {
            mIndicatorDrawable = null;
        }