The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.Gravity; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 27 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | /** |
| 31 | * An extension to TextView that supports the {@link android.widget.Checkable} interface. |
| 32 | * This is useful when used in a {@link android.widget.ListView ListView} where the it's |
| 33 | * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to |
| 34 | * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}. |
| 35 | * |
| 36 | */ |
Romain Guy | 8b5e7c0 | 2009-04-29 11:48:22 -0700 | [diff] [blame] | 37 | public class CheckedTextView extends TextView implements Checkable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | private boolean mChecked; |
| 39 | private int mCheckMarkResource; |
| 40 | private Drawable mCheckMarkDrawable; |
| 41 | private int mBasePaddingRight; |
| 42 | private int mCheckMarkWidth; |
| 43 | |
| 44 | private static final int[] CHECKED_STATE_SET = { |
| 45 | R.attr.state_checked |
| 46 | }; |
| 47 | |
| 48 | public CheckedTextView(Context context) { |
| 49 | this(context, null); |
| 50 | } |
| 51 | |
| 52 | public CheckedTextView(Context context, AttributeSet attrs) { |
| 53 | this(context, attrs, 0); |
| 54 | } |
| 55 | |
| 56 | public CheckedTextView(Context context, AttributeSet attrs, int defStyle) { |
| 57 | super(context, attrs, defStyle); |
| 58 | |
| 59 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 60 | R.styleable.CheckedTextView, defStyle, 0); |
| 61 | |
| 62 | Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark); |
| 63 | if (d != null) { |
| 64 | setCheckMarkDrawable(d); |
| 65 | } |
| 66 | |
| 67 | boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false); |
| 68 | setChecked(checked); |
| 69 | |
| 70 | a.recycle(); |
| 71 | } |
| 72 | |
| 73 | public void toggle() { |
| 74 | setChecked(!mChecked); |
| 75 | } |
| 76 | |
| 77 | public boolean isChecked() { |
| 78 | return mChecked; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * <p>Changes the checked state of this text view.</p> |
| 83 | * |
| 84 | * @param checked true to check the text, false to uncheck it |
| 85 | */ |
| 86 | public void setChecked(boolean checked) { |
| 87 | if (mChecked != checked) { |
| 88 | mChecked = checked; |
| 89 | refreshDrawableState(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn |
| 96 | * when {@link #isChecked()} is true. |
| 97 | * |
| 98 | * @param resid The Drawable to use for the checkmark. |
| 99 | */ |
| 100 | public void setCheckMarkDrawable(int resid) { |
| 101 | if (resid != 0 && resid == mCheckMarkResource) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | mCheckMarkResource = resid; |
| 106 | |
| 107 | Drawable d = null; |
| 108 | if (mCheckMarkResource != 0) { |
| 109 | d = getResources().getDrawable(mCheckMarkResource); |
| 110 | } |
| 111 | setCheckMarkDrawable(d); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true. |
| 116 | * |
| 117 | * @param d The Drawable to use for the checkmark. |
| 118 | */ |
| 119 | public void setCheckMarkDrawable(Drawable d) { |
Leon Scroggins | a8da173 | 2009-10-19 19:04:30 -0400 | [diff] [blame^] | 120 | if (mCheckMarkDrawable != null) { |
| 121 | mCheckMarkDrawable.setCallback(null); |
| 122 | unscheduleDrawable(mCheckMarkDrawable); |
| 123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | if (d != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | d.setCallback(this); |
| 126 | d.setVisible(getVisibility() == VISIBLE, false); |
| 127 | d.setState(CHECKED_STATE_SET); |
| 128 | setMinHeight(d.getIntrinsicHeight()); |
| 129 | |
| 130 | mCheckMarkWidth = d.getIntrinsicWidth(); |
| 131 | mPaddingRight = mCheckMarkWidth + mBasePaddingRight; |
| 132 | d.setState(getDrawableState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } else { |
| 134 | mPaddingRight = mBasePaddingRight; |
| 135 | } |
Leon Scroggins | a8da173 | 2009-10-19 19:04:30 -0400 | [diff] [blame^] | 136 | mCheckMarkDrawable = d; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | requestLayout(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void setPadding(int left, int top, int right, int bottom) { |
| 142 | super.setPadding(left, top, right, bottom); |
| 143 | mBasePaddingRight = mPaddingRight; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected void onDraw(Canvas canvas) { |
| 148 | super.onDraw(canvas); |
| 149 | |
| 150 | final Drawable checkMarkDrawable = mCheckMarkDrawable; |
| 151 | if (checkMarkDrawable != null) { |
| 152 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
| 153 | final int height = checkMarkDrawable.getIntrinsicHeight(); |
| 154 | |
| 155 | int y = 0; |
| 156 | |
| 157 | switch (verticalGravity) { |
| 158 | case Gravity.BOTTOM: |
| 159 | y = getHeight() - height; |
| 160 | break; |
| 161 | case Gravity.CENTER_VERTICAL: |
| 162 | y = (getHeight() - height) / 2; |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | int right = getWidth(); |
| 167 | checkMarkDrawable.setBounds( |
| 168 | right - mCheckMarkWidth - mBasePaddingRight, |
| 169 | y, |
| 170 | right - mBasePaddingRight, |
| 171 | y + height); |
| 172 | checkMarkDrawable.draw(canvas); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected int[] onCreateDrawableState(int extraSpace) { |
| 178 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 179 | if (isChecked()) { |
| 180 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 181 | } |
| 182 | return drawableState; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | protected void drawableStateChanged() { |
| 187 | super.drawableStateChanged(); |
| 188 | |
| 189 | if (mCheckMarkDrawable != null) { |
| 190 | int[] myDrawableState = getDrawableState(); |
| 191 | |
| 192 | // Set the state of the Drawable |
| 193 | mCheckMarkDrawable.setState(myDrawableState); |
| 194 | |
| 195 | invalidate(); |
| 196 | } |
| 197 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 198 | |
| 199 | @Override |
| 200 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 201 | boolean populated = super.dispatchPopulateAccessibilityEvent(event); |
| 202 | if (!populated) { |
| 203 | event.setChecked(mChecked); |
| 204 | } |
| 205 | return populated; |
| 206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |