diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:31:44 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:31:44 -0800 |
commit | 9066cfe9886ac131c34d59ed0e2d287b0e3c0087 (patch) | |
tree | d88beb88001f2482911e3d28e43833b50e4b4e97 /core/java/android/widget/CheckedTextView.java | |
parent | d83a98f4ce9cfa908f5c54bbd70f03eec07e7553 (diff) | |
download | frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.zip frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.tar.gz frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.tar.bz2 |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'core/java/android/widget/CheckedTextView.java')
-rw-r--r-- | core/java/android/widget/CheckedTextView.java | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/core/java/android/widget/CheckedTextView.java b/core/java/android/widget/CheckedTextView.java new file mode 100644 index 0000000..f5a0b1c --- /dev/null +++ b/core/java/android/widget/CheckedTextView.java @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.view.Gravity; + +import com.android.internal.R; + + +/** + * 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}. + * + */ +public abstract class CheckedTextView extends TextView implements Checkable { + private boolean mChecked; + private int mCheckMarkResource; + private Drawable mCheckMarkDrawable; + private int mBasePaddingRight; + private int mCheckMarkWidth; + + private static final int[] CHECKED_STATE_SET = { + R.attr.state_checked + }; + + public CheckedTextView(Context context) { + this(context, null); + } + + public CheckedTextView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public CheckedTextView(Context context, 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(); + } + + public void toggle() { + setChecked(!mChecked); + } + + public boolean isChecked() { + return mChecked; + } + + /** + * <p>Changes the checked state of this text view.</p> + * + * @param checked true to check the text, false to uncheck it + */ + public void setChecked(boolean checked) { + if (mChecked != checked) { + mChecked = checked; + refreshDrawableState(); + } + } + + + /** + * 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. + */ + public void setCheckMarkDrawable(int resid) { + if (resid != 0 && resid == mCheckMarkResource) { + return; + } + + mCheckMarkResource = resid; + + Drawable d = null; + if (mCheckMarkResource != 0) { + d = getResources().getDrawable(mCheckMarkResource); + } + setCheckMarkDrawable(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. + */ + public void setCheckMarkDrawable(Drawable d) { + 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(); + } + + @Override + public void setPadding(int left, int top, int right, int bottom) { + super.setPadding(left, top, right, bottom); + mBasePaddingRight = mPaddingRight; + } + + @Override + protected void onDraw(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); + } + } + + @Override + protected int[] onCreateDrawableState(int extraSpace) { + final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); + if (isChecked()) { + mergeDrawableStates(drawableState, CHECKED_STATE_SET); + } + return drawableState; + } + + @Override + protected void drawableStateChanged() { + super.drawableStateChanged(); + + if (mCheckMarkDrawable != null) { + int[] myDrawableState = getDrawableState(); + + // Set the state of the Drawable + mCheckMarkDrawable.setState(myDrawableState); + + invalidate(); + } + } + +} |