CheckedTextView.java revision b023a587adf9ca49f0cc33c5607b535c11efeb2b
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 17package android.widget; 18 19import com.android.internal.R; 20 21import android.content.Context; 22import android.content.res.TypedArray; 23import android.graphics.Canvas; 24import android.graphics.drawable.Drawable; 25import android.util.AttributeSet; 26import android.view.Gravity; 27import android.view.ViewDebug; 28import android.view.accessibility.AccessibilityEvent; 29import android.view.accessibility.AccessibilityNodeInfo; 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 * 38 * @attr ref android.R.styleable#CheckedTextView_checked 39 * @attr ref android.R.styleable#CheckedTextView_checkMark 40 */ 41public class CheckedTextView extends TextView implements Checkable { 42 private boolean mChecked; 43 private int mCheckMarkResource; 44 private Drawable mCheckMarkDrawable; 45 private int mBasePadding; 46 private int mCheckMarkWidth; 47 private boolean mNeedRequestlayout; 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) { 58 this(context, attrs, R.attr.checkedTextViewStyle); 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 } 81 82 @ViewDebug.ExportedProperty 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(); 96 notifyAccessibilityStateChanged(); 97 } 98 } 99 100 101 /** 102 * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn 103 * when {@link #isChecked()} is true. 104 * 105 * @param resid The Drawable to use for the checkmark. 106 * 107 * @see #setCheckMarkDrawable(Drawable) 108 * @see #getCheckMarkDrawable() 109 * 110 * @attr ref android.R.styleable#CheckedTextView_checkMark 111 */ 112 public void setCheckMarkDrawable(int resid) { 113 if (resid != 0 && resid == mCheckMarkResource) { 114 return; 115 } 116 117 mCheckMarkResource = resid; 118 119 Drawable d = null; 120 if (mCheckMarkResource != 0) { 121 d = getResources().getDrawable(mCheckMarkResource); 122 } 123 setCheckMarkDrawable(d); 124 } 125 126 /** 127 * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true. 128 * 129 * @param d The Drawable to use for the checkmark. 130 * 131 * @see #setCheckMarkDrawable(int) 132 * @see #getCheckMarkDrawable() 133 * 134 * @attr ref android.R.styleable#CheckedTextView_checkMark 135 */ 136 public void setCheckMarkDrawable(Drawable d) { 137 if (mCheckMarkDrawable != null) { 138 mCheckMarkDrawable.setCallback(null); 139 unscheduleDrawable(mCheckMarkDrawable); 140 } 141 mNeedRequestlayout = (d != mCheckMarkDrawable); 142 if (d != null) { 143 d.setCallback(this); 144 d.setVisible(getVisibility() == VISIBLE, false); 145 d.setState(CHECKED_STATE_SET); 146 setMinHeight(d.getIntrinsicHeight()); 147 148 mCheckMarkWidth = d.getIntrinsicWidth(); 149 d.setState(getDrawableState()); 150 } else { 151 mCheckMarkWidth = 0; 152 } 153 mCheckMarkDrawable = d; 154 // Do padding resolution. This will call internalSetPadding() and do a requestLayout() if needed. 155 resolvePadding(); 156 } 157 158 /** 159 * Gets the checkmark drawable 160 * 161 * @return The drawable use to represent the checkmark, if any. 162 * 163 * @see #setCheckMarkDrawable(Drawable) 164 * @see #setCheckMarkDrawable(int) 165 * 166 * @attr ref android.R.styleable#CheckedTextView_checkMark 167 */ 168 public Drawable getCheckMarkDrawable() { 169 return mCheckMarkDrawable; 170 } 171 172 /** 173 * @hide 174 */ 175 @Override 176 protected void internalSetPadding(int left, int top, int right, int bottom) { 177 super.internalSetPadding(left, top, right, bottom); 178 setBasePadding(isLayoutRtl()); 179 } 180 181 @Override 182 public void onRtlPropertiesChanged(int layoutDirection) { 183 super.onRtlPropertiesChanged(layoutDirection); 184 updatePadding(); 185 } 186 187 private void updatePadding() { 188 resetPaddingToInitialValues(); 189 int newPadding = (mCheckMarkDrawable != null) ? 190 mCheckMarkWidth + mBasePadding : mBasePadding; 191 mNeedRequestlayout |= (mPaddingRight != newPadding); 192 if (isLayoutRtl()) { 193 mPaddingLeft = newPadding; 194 } else { 195 mPaddingRight = newPadding; 196 } 197 if (mNeedRequestlayout) { 198 requestLayout(); 199 mNeedRequestlayout = false; 200 } 201 } 202 203 @Override 204 public void setPadding(int left, int top, int right, int bottom) { 205 super.setPadding(left, top, right, bottom); 206 setBasePadding(isLayoutRtl()); 207 } 208 209 @Override 210 public void setPaddingRelative(int start, int top, int end, int bottom) { 211 super.setPaddingRelative(start, top, end, bottom); 212 setBasePadding(isLayoutRtl()); 213 } 214 215 private void setBasePadding(boolean isLayoutRtl) { 216 if (isLayoutRtl) { 217 mBasePadding = mPaddingLeft; 218 } else { 219 mBasePadding = mPaddingRight; 220 } 221 } 222 223 @Override 224 protected void onDraw(Canvas canvas) { 225 super.onDraw(canvas); 226 227 final Drawable checkMarkDrawable = mCheckMarkDrawable; 228 if (checkMarkDrawable != null) { 229 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; 230 final int height = checkMarkDrawable.getIntrinsicHeight(); 231 232 int y = 0; 233 234 switch (verticalGravity) { 235 case Gravity.BOTTOM: 236 y = getHeight() - height; 237 break; 238 case Gravity.CENTER_VERTICAL: 239 y = (getHeight() - height) / 2; 240 break; 241 } 242 243 final boolean isLayoutRtl = isLayoutRtl(); 244 final int width = getWidth(); 245 final int top = y; 246 final int bottom = top + height; 247 final int left; 248 final int right; 249 if (isLayoutRtl) { 250 left = mBasePadding; 251 right = left + mCheckMarkWidth; 252 } else { 253 right = width - mBasePadding; 254 left = right - mCheckMarkWidth; 255 } 256 checkMarkDrawable.setBounds( left, top, right, bottom); 257 checkMarkDrawable.draw(canvas); 258 } 259 } 260 261 @Override 262 protected int[] onCreateDrawableState(int extraSpace) { 263 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 264 if (isChecked()) { 265 mergeDrawableStates(drawableState, CHECKED_STATE_SET); 266 } 267 return drawableState; 268 } 269 270 @Override 271 protected void drawableStateChanged() { 272 super.drawableStateChanged(); 273 274 if (mCheckMarkDrawable != null) { 275 int[] myDrawableState = getDrawableState(); 276 277 // Set the state of the Drawable 278 mCheckMarkDrawable.setState(myDrawableState); 279 280 invalidate(); 281 } 282 } 283 284 @Override 285 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 286 super.onInitializeAccessibilityEvent(event); 287 event.setClassName(CheckedTextView.class.getName()); 288 event.setChecked(mChecked); 289 } 290 291 @Override 292 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 293 super.onInitializeAccessibilityNodeInfo(info); 294 info.setClassName(CheckedTextView.class.getName()); 295 info.setCheckable(true); 296 info.setChecked(mChecked); 297 } 298} 299