FileDocCategorySizeDatePackage
CheckedTextView.javaAPI DocAndroid 1.5 API6130Wed May 06 22:41:56 BST 2009android.widget

CheckedTextView

public abstract class CheckedTextView extends TextView implements Checkable
An extension to TextView that supports the {@link android.widget.Checkable} interface. This is useful when used in a {@link android.widget.ListView ListView} where the it's {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.

Fields Summary
private boolean
mChecked
private int
mCheckMarkResource
private android.graphics.drawable.Drawable
mCheckMarkDrawable
private int
mBasePaddingRight
private int
mCheckMarkWidth
private static final int[]
CHECKED_STATE_SET
Constructors Summary
public CheckedTextView(android.content.Context context)


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

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

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CheckedTextView, defStyle, 0);

        Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
        if (d != null) {
            setCheckMarkDrawable(d);
        }

        boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
        setChecked(checked);

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

        super.drawableStateChanged();
        
        if (mCheckMarkDrawable != null) {
            int[] myDrawableState = getDrawableState();
            
            // Set the state of the Drawable
            mCheckMarkDrawable.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 checkMarkDrawable = mCheckMarkDrawable;
        if (checkMarkDrawable != null) {
            final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
            final int height = checkMarkDrawable.getIntrinsicHeight();

            int y = 0;

            switch (verticalGravity) {
                case Gravity.BOTTOM:
                    y = getHeight() - height;
                    break;
                case Gravity.CENTER_VERTICAL:
                    y = (getHeight() - height) / 2;
                    break;
            }
            
            int right = getWidth();
            checkMarkDrawable.setBounds(
                    right - mCheckMarkWidth - mBasePaddingRight, 
                    y, 
                    right - mBasePaddingRight, 
                    y + height);
            checkMarkDrawable.draw(canvas);
        }
    
public voidsetCheckMarkDrawable(int resid)
Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn when {@link #isChecked()} is true.

param
resid The Drawable to use for the checkmark.

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

        mCheckMarkResource = resid;

        Drawable d = null;
        if (mCheckMarkResource != 0) {
            d = getResources().getDrawable(mCheckMarkResource);
        }
        setCheckMarkDrawable(d);
    
public voidsetCheckMarkDrawable(android.graphics.drawable.Drawable d)
Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.

param
d The Drawable to use for the checkmark.

        if (d != null) {
            if (mCheckMarkDrawable != null) {
                mCheckMarkDrawable.setCallback(null);
                unscheduleDrawable(mCheckMarkDrawable);
            }
            d.setCallback(this);
            d.setVisible(getVisibility() == VISIBLE, false);
            d.setState(CHECKED_STATE_SET);
            setMinHeight(d.getIntrinsicHeight());
            
            mCheckMarkWidth = d.getIntrinsicWidth();
            mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
            d.setState(getDrawableState());
            mCheckMarkDrawable = d;
        } else {
            mPaddingRight = mBasePaddingRight;
        }
        requestLayout();
    
public voidsetChecked(boolean checked)

Changes the checked state of this text view.

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

        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }
    
public voidsetPadding(int left, int top, int right, int bottom)

        super.setPadding(left, top, right, bottom);
        mBasePaddingRight = mPaddingRight;
    
public voidtoggle()

        setChecked(!mChecked);