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