FileDocCategorySizeDatePackage
ToggleButton.javaAPI DocAndroid 1.5 API4386Wed May 06 22:41:56 BST 2009android.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".
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 defStyle)

    
           
        super(context, attrs, defStyle);
        
        TypedArray a =
            context.obtainStyledAttributes(
                    attrs, com.android.internal.R.styleable.ToggleButton, defStyle, 0);
        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)

        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 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);
        }