FileDocCategorySizeDatePackage
CompoundButton.javaAPI DocAndroid 1.5 API9836Wed May 06 22:41:56 BST 2009android.widget

CompoundButton

public abstract class CompoundButton extends Button implements Checkable

A button with two states, checked and unchecked. When the button is pressed or clicked, the state changes automatically.

XML attributes

See {@link android.R.styleable#CompoundButton CompoundButton Attributes}, {@link android.R.styleable#Button Button Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link android.R.styleable#View View Attributes}

Fields Summary
private boolean
mChecked
private int
mButtonResource
private boolean
mBroadcasting
private android.graphics.drawable.Drawable
mButtonDrawable
private OnCheckedChangeListener
mOnCheckedChangeListener
private OnCheckedChangeListener
mOnCheckedChangeWidgetListener
private static final int[]
CHECKED_STATE_SET
Constructors Summary
public CompoundButton(android.content.Context context)


       
        this(context, null);
    
public CompoundButton(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, 0);
    
public CompoundButton(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        TypedArray a =
                context.obtainStyledAttributes(
                        attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);

        Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
        if (d != null) {
            setButtonDrawable(d);
        }

        boolean checked = a
                .getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);
        setChecked(checked);

        a.recycle();
    
Methods Summary
protected voiddrawableStateChanged()

        super.drawableStateChanged();
        
        if (mButtonDrawable != null) {
            int[] myDrawableState = getDrawableState();
            
            // Set the state of the Drawable
            mButtonDrawable.setState(myDrawableState);
            
            invalidate();
        }
    
public booleanisChecked()

        return mChecked;
    
protected int[]onCreateDrawableState(int extraSpace)

        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    
protected voidonDraw(android.graphics.Canvas canvas)

        super.onDraw(canvas);

        final Drawable buttonDrawable = mButtonDrawable;
        if (buttonDrawable != null) {
            final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
            final int height = buttonDrawable.getIntrinsicHeight();

            int y = 0;

            switch (verticalGravity) {
                case Gravity.BOTTOM:
                    y = getHeight() - height;
                    break;
                case Gravity.CENTER_VERTICAL:
                    y = (getHeight() - height) / 2;
                    break;
            }

            buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);
            buttonDrawable.draw(canvas);
        }
    
public voidonRestoreInstanceState(android.os.Parcelable state)

        SavedState ss = (SavedState) state;
  
        super.onRestoreInstanceState(ss.getSuperState());
        setChecked(ss.checked);
        requestLayout();
    
public android.os.ParcelableonSaveInstanceState()

    

    
       
        // Force our ancestor class to save its state
        setFreezesText(true);
        Parcelable superState = super.onSaveInstanceState();

        SavedState ss = new SavedState(superState);

        ss.checked = isChecked();
        return ss;
    
public booleanperformClick()

        /*
         * XXX: These are tiny, need some surrounding 'expanded touch area',
         * which will need to be implemented in Button if we only override
         * performClick()
         */

        /* When clicked, toggle the state */
        toggle();
        return super.performClick();
    
public voidsetButtonDrawable(int resid)
Set the background to a given Drawable, identified by its resource id.

param
resid the resource id of the drawable to use as the background

        if (resid != 0 && resid == mButtonResource) {
            return;
        }

        mButtonResource = resid;

        Drawable d = null;
        if (mButtonResource != 0) {
            d = getResources().getDrawable(mButtonResource);
        }
        setButtonDrawable(d);
    
public voidsetButtonDrawable(android.graphics.drawable.Drawable d)
Set the background to a given Drawable

param
d The Drawable to use as the background

        if (d != null) {
            if (mButtonDrawable != null) {
                mButtonDrawable.setCallback(null);
                unscheduleDrawable(mButtonDrawable);
            }
            d.setCallback(this);
            d.setState(getDrawableState());
            d.setVisible(getVisibility() == VISIBLE, false);
            mButtonDrawable = d;
            mButtonDrawable.setState(null);
            setMinHeight(mButtonDrawable.getIntrinsicHeight());
        }

        refreshDrawableState();
    
public voidsetChecked(boolean checked)

Changes the checked state of this button.

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

        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();

            // Avoid infinite recursions if setChecked() is called from a listener
            if (mBroadcasting) {
                return;
            }

            mBroadcasting = true;
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
            }
            if (mOnCheckedChangeWidgetListener != null) {
                mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
            }
            mBroadcasting = false;            
        }
    
public voidsetOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener listener)
Register a callback to be invoked when the checked state of this button changes.

param
listener the callback to call on checked state change

        mOnCheckedChangeListener = listener;
    
voidsetOnCheckedChangeWidgetListener(android.widget.CompoundButton$OnCheckedChangeListener listener)
Register a callback to be invoked when the checked state of this button changes. This callback is used for internal purpose only.

param
listener the callback to call on checked state change
hide

        mOnCheckedChangeWidgetListener = listener;
    
public voidtoggle()

        setChecked(!mChecked);
    
protected booleanverifyDrawable(android.graphics.drawable.Drawable who)

        return super.verifyDrawable(who) || who == mButtonDrawable;