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; |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 27 | import android.view.ViewDebug; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 28 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 34ffaab | 2011-09-12 16:19:32 -0700 | [diff] [blame] | 29 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /** |
| 33 | * An extension to TextView that supports the {@link android.widget.Checkable} interface. |
| 34 | * This is useful when used in a {@link android.widget.ListView ListView} where the it's |
| 35 | * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to |
| 36 | * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}. |
| 37 | * |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 38 | * @attr ref android.R.styleable#CheckedTextView_checked |
| 39 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | */ |
Romain Guy | 8b5e7c0 | 2009-04-29 11:48:22 -0700 | [diff] [blame] | 41 | public class CheckedTextView extends TextView implements Checkable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | private boolean mChecked; |
| 43 | private int mCheckMarkResource; |
| 44 | private Drawable mCheckMarkDrawable; |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 45 | private int mBasePadding; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | private int mCheckMarkWidth; |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 47 | private boolean mNeedRequestlayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Fabrice Di Meglio | 15bbde4 | 2012-09-28 15:49:38 -0700 | [diff] [blame] | 49 | private int initialPaddingLeft = -1; |
| 50 | private int initialPaddingRight = -1; |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | private static final int[] CHECKED_STATE_SET = { |
| 53 | R.attr.state_checked |
| 54 | }; |
| 55 | |
| 56 | public CheckedTextView(Context context) { |
| 57 | this(context, null); |
| 58 | } |
| 59 | |
| 60 | public CheckedTextView(Context context, AttributeSet attrs) { |
| 61 | this(context, attrs, 0); |
| 62 | } |
| 63 | |
| 64 | public CheckedTextView(Context context, AttributeSet attrs, int defStyle) { |
| 65 | super(context, attrs, defStyle); |
| 66 | |
| 67 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 68 | R.styleable.CheckedTextView, defStyle, 0); |
| 69 | |
| 70 | Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark); |
| 71 | if (d != null) { |
| 72 | setCheckMarkDrawable(d); |
| 73 | } |
| 74 | |
| 75 | boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false); |
| 76 | setChecked(checked); |
| 77 | |
| 78 | a.recycle(); |
| 79 | } |
| 80 | |
| 81 | public void toggle() { |
| 82 | setChecked(!mChecked); |
| 83 | } |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 84 | |
| 85 | @ViewDebug.ExportedProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | public boolean isChecked() { |
| 87 | return mChecked; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * <p>Changes the checked state of this text view.</p> |
| 92 | * |
| 93 | * @param checked true to check the text, false to uncheck it |
| 94 | */ |
| 95 | public void setChecked(boolean checked) { |
| 96 | if (mChecked != checked) { |
| 97 | mChecked = checked; |
| 98 | refreshDrawableState(); |
Svetoslav Ganov | c406be9 | 2012-05-11 16:12:32 -0700 | [diff] [blame] | 99 | notifyAccessibilityStateChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn |
| 106 | * when {@link #isChecked()} is true. |
Fabrice Di Meglio | 343e113 | 2012-09-28 18:01:17 -0700 | [diff] [blame^] | 107 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | * @param resid The Drawable to use for the checkmark. |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 109 | * |
| 110 | * @see #setCheckMarkDrawable(Drawable) |
| 111 | * @see #getCheckMarkDrawable() |
| 112 | * |
| 113 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | */ |
| 115 | public void setCheckMarkDrawable(int resid) { |
| 116 | if (resid != 0 && resid == mCheckMarkResource) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | mCheckMarkResource = resid; |
| 121 | |
| 122 | Drawable d = null; |
| 123 | if (mCheckMarkResource != 0) { |
| 124 | d = getResources().getDrawable(mCheckMarkResource); |
| 125 | } |
| 126 | setCheckMarkDrawable(d); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true. |
| 131 | * |
| 132 | * @param d The Drawable to use for the checkmark. |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 133 | * |
| 134 | * @see #setCheckMarkDrawable(int) |
| 135 | * @see #getCheckMarkDrawable() |
| 136 | * |
| 137 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | */ |
| 139 | public void setCheckMarkDrawable(Drawable d) { |
Leon Scroggins | a8da173 | 2009-10-19 19:04:30 -0400 | [diff] [blame] | 140 | if (mCheckMarkDrawable != null) { |
| 141 | mCheckMarkDrawable.setCallback(null); |
| 142 | unscheduleDrawable(mCheckMarkDrawable); |
| 143 | } |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 144 | mNeedRequestlayout = (d != mCheckMarkDrawable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | if (d != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | d.setCallback(this); |
| 147 | d.setVisible(getVisibility() == VISIBLE, false); |
| 148 | d.setState(CHECKED_STATE_SET); |
| 149 | setMinHeight(d.getIntrinsicHeight()); |
Fabrice Di Meglio | 343e113 | 2012-09-28 18:01:17 -0700 | [diff] [blame^] | 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | mCheckMarkWidth = d.getIntrinsicWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | d.setState(getDrawableState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } else { |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 154 | mCheckMarkWidth = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
Leon Scroggins | a8da173 | 2009-10-19 19:04:30 -0400 | [diff] [blame] | 156 | mCheckMarkDrawable = d; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 157 | // Do padding resolution. This will call internalSetPadding() and do a requestLayout() if needed. |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 158 | resolvePadding(); |
| 159 | } |
| 160 | |
Gilles Debunne | 2fb4028 | 2012-05-01 12:07:06 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Gets the checkmark drawable |
| 163 | * |
| 164 | * @return The drawable use to represent the checkmark, if any. |
| 165 | * |
| 166 | * @see #setCheckMarkDrawable(Drawable) |
| 167 | * @see #setCheckMarkDrawable(int) |
| 168 | * |
| 169 | * @attr ref android.R.styleable#CheckedTextView_checkMark |
| 170 | */ |
| 171 | public Drawable getCheckMarkDrawable() { |
| 172 | return mCheckMarkDrawable; |
| 173 | } |
| 174 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 175 | /** |
| 176 | * @hide |
| 177 | */ |
| 178 | @Override |
| 179 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
| 180 | super.internalSetPadding(left, top, right, bottom); |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 181 | setBasePadding(isLayoutRtl()); |
Fabrice Di Meglio | 15bbde4 | 2012-09-28 15:49:38 -0700 | [diff] [blame] | 182 | initialPaddingLeft = mPaddingLeft; |
| 183 | initialPaddingRight = mPaddingRight; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 186 | @Override |
Fabrice Di Meglio | 343e113 | 2012-09-28 18:01:17 -0700 | [diff] [blame^] | 187 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 188 | super.onRtlPropertiesChanged(layoutDirection); |
Fabrice Di Meglio | 15bbde4 | 2012-09-28 15:49:38 -0700 | [diff] [blame] | 189 | updatePadding(); |
| 190 | } |
| 191 | |
| 192 | private void updatePadding() { |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 193 | int newPadding = (mCheckMarkDrawable != null) ? |
| 194 | mCheckMarkWidth + mBasePadding : mBasePadding; |
| 195 | mNeedRequestlayout |= (mPaddingRight != newPadding); |
Fabrice Di Meglio | e2386c1 | 2012-07-26 15:17:16 -0700 | [diff] [blame] | 196 | if (isLayoutRtl()) { |
| 197 | mPaddingLeft = newPadding; |
| 198 | } else { |
| 199 | mPaddingRight = newPadding; |
| 200 | } |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 201 | if (mNeedRequestlayout) { |
| 202 | requestLayout(); |
| 203 | mNeedRequestlayout = false; |
| 204 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } |
Fabrice Di Meglio | 15bbde4 | 2012-09-28 15:49:38 -0700 | [diff] [blame] | 206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | @Override |
| 208 | public void setPadding(int left, int top, int right, int bottom) { |
| 209 | super.setPadding(left, top, right, bottom); |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 210 | setBasePadding(isLayoutRtl()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
Fabrice Di Meglio | bf923eb | 2012-03-07 16:20:22 -0800 | [diff] [blame] | 214 | public void setPaddingRelative(int start, int top, int end, int bottom) { |
| 215 | super.setPaddingRelative(start, top, end, bottom); |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 216 | setBasePadding(isLayoutRtl()); |
| 217 | } |
| 218 | |
| 219 | private void setBasePadding(boolean isLayoutRtl) { |
| 220 | if (isLayoutRtl) { |
| 221 | mBasePadding = mPaddingLeft; |
| 222 | } else { |
| 223 | mBasePadding = mPaddingRight; |
| 224 | } |
Fabrice Di Meglio | bf923eb | 2012-03-07 16:20:22 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | protected void onDraw(Canvas canvas) { |
| 229 | super.onDraw(canvas); |
| 230 | |
| 231 | final Drawable checkMarkDrawable = mCheckMarkDrawable; |
| 232 | if (checkMarkDrawable != null) { |
| 233 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
| 234 | final int height = checkMarkDrawable.getIntrinsicHeight(); |
| 235 | |
| 236 | int y = 0; |
| 237 | |
| 238 | switch (verticalGravity) { |
| 239 | case Gravity.BOTTOM: |
| 240 | y = getHeight() - height; |
| 241 | break; |
| 242 | case Gravity.CENTER_VERTICAL: |
| 243 | y = (getHeight() - height) / 2; |
| 244 | break; |
| 245 | } |
| 246 | |
Fabrice Di Meglio | e2386c1 | 2012-07-26 15:17:16 -0700 | [diff] [blame] | 247 | final boolean isLayoutRtl = isLayoutRtl(); |
| 248 | final int width = getWidth(); |
| 249 | final int top = y; |
| 250 | final int bottom = top + height; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 251 | final int left; |
| 252 | final int right; |
| 253 | if (isLayoutRtl) { |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 254 | left = mBasePadding; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 255 | right = left + mCheckMarkWidth; |
Fabrice Di Meglio | 0dc9646 | 2012-08-24 12:25:28 -0700 | [diff] [blame] | 256 | } else { |
| 257 | right = width - mBasePadding; |
| 258 | left = right - mCheckMarkWidth; |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 259 | } |
Fabrice Di Meglio | e2386c1 | 2012-07-26 15:17:16 -0700 | [diff] [blame] | 260 | checkMarkDrawable.setBounds( left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | checkMarkDrawable.draw(canvas); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | protected int[] onCreateDrawableState(int extraSpace) { |
| 267 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 268 | if (isChecked()) { |
| 269 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 270 | } |
| 271 | return drawableState; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | protected void drawableStateChanged() { |
| 276 | super.drawableStateChanged(); |
| 277 | |
| 278 | if (mCheckMarkDrawable != null) { |
| 279 | int[] myDrawableState = getDrawableState(); |
| 280 | |
| 281 | // Set the state of the Drawable |
| 282 | mCheckMarkDrawable.setState(myDrawableState); |
| 283 | |
| 284 | invalidate(); |
| 285 | } |
| 286 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 287 | |
| 288 | @Override |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 289 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 290 | super.onInitializeAccessibilityEvent(event); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 291 | event.setClassName(CheckedTextView.class.getName()); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 292 | event.setChecked(mChecked); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 293 | } |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 294 | |
| 295 | @Override |
Svetoslav Ganov | 34ffaab | 2011-09-12 16:19:32 -0700 | [diff] [blame] | 296 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 297 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 298 | info.setClassName(CheckedTextView.class.getName()); |
Svetoslav Ganov | c85015c | 2012-04-30 11:37:43 -0700 | [diff] [blame] | 299 | info.setCheckable(true); |
Svetoslav Ganov | 34ffaab | 2011-09-12 16:19:32 -0700 | [diff] [blame] | 300 | info.setChecked(mChecked); |
| 301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |