CheckedTextView.java revision c85015cd0409b5ffd55bf94a068828385d032f47
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 */ 39public class CheckedTextView extends TextView implements Checkable { 40 private boolean mChecked; 41 private int mCheckMarkResource; 42 private Drawable mCheckMarkDrawable; 43 private int mBasePadding; 44 private int mCheckMarkWidth; 45 private boolean mNeedRequestlayout; 46 47 private static final int[] CHECKED_STATE_SET = { 48 R.attr.state_checked 49 }; 50 51 public CheckedTextView(Context context) { 52 this(context, null); 53 } 54 55 public CheckedTextView(Context context, AttributeSet attrs) { 56 this(context, attrs, 0); 57 } 58 59 public CheckedTextView(Context context, AttributeSet attrs, int defStyle) { 60 super(context, attrs, defStyle); 61 62 TypedArray a = context.obtainStyledAttributes(attrs, 63 R.styleable.CheckedTextView, defStyle, 0); 64 65 Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark); 66 if (d != null) { 67 setCheckMarkDrawable(d); 68 } 69 70 boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false); 71 setChecked(checked); 72 73 a.recycle(); 74 } 75 76 public void toggle() { 77 setChecked(!mChecked); 78 } 79 80 @ViewDebug.ExportedProperty 81 public boolean isChecked() { 82 return mChecked; 83 } 84 85 /** 86 * <p>Changes the checked state of this text view.</p> 87 * 88 * @param checked true to check the text, false to uncheck it 89 */ 90 public void setChecked(boolean checked) { 91 if (mChecked != checked) { 92 mChecked = checked; 93 refreshDrawableState(); 94 } 95 } 96 97 98 /** 99 * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn 100 * when {@link #isChecked()} is true. 101 * 102 * @param resid The Drawable to use for the checkmark. 103 */ 104 public void setCheckMarkDrawable(int resid) { 105 if (resid != 0 && resid == mCheckMarkResource) { 106 return; 107 } 108 109 mCheckMarkResource = resid; 110 111 Drawable d = null; 112 if (mCheckMarkResource != 0) { 113 d = getResources().getDrawable(mCheckMarkResource); 114 } 115 setCheckMarkDrawable(d); 116 } 117 118 /** 119 * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true. 120 * 121 * @param d The Drawable to use for the checkmark. 122 */ 123 public void setCheckMarkDrawable(Drawable d) { 124 if (mCheckMarkDrawable != null) { 125 mCheckMarkDrawable.setCallback(null); 126 unscheduleDrawable(mCheckMarkDrawable); 127 } 128 mNeedRequestlayout = (d != mCheckMarkDrawable); 129 if (d != null) { 130 d.setCallback(this); 131 d.setVisible(getVisibility() == VISIBLE, false); 132 d.setState(CHECKED_STATE_SET); 133 setMinHeight(d.getIntrinsicHeight()); 134 135 mCheckMarkWidth = d.getIntrinsicWidth(); 136 d.setState(getDrawableState()); 137 } else { 138 mCheckMarkWidth = 0; 139 } 140 mCheckMarkDrawable = d; 141 // Do padding resolution. This will call setPadding() and do a requestLayout() if needed. 142 resolvePadding(); 143 } 144 145 @Override 146 public void onPaddingChanged(int layoutDirection) { 147 int newPadding = (mCheckMarkDrawable != null) ? 148 mCheckMarkWidth + mBasePadding : mBasePadding; 149 mNeedRequestlayout |= (mPaddingRight != newPadding); 150 mPaddingRight = newPadding; 151 if (mNeedRequestlayout) { 152 requestLayout(); 153 mNeedRequestlayout = false; 154 } 155 } 156 157 @Override 158 public void setPadding(int left, int top, int right, int bottom) { 159 super.setPadding(left, top, right, bottom); 160 mBasePadding = mPaddingRight; 161 } 162 163 @Override 164 public void setPaddingRelative(int start, int top, int end, int bottom) { 165 super.setPaddingRelative(start, top, end, bottom); 166 mBasePadding = getPaddingEnd(); 167 } 168 169 @Override 170 protected void onDraw(Canvas canvas) { 171 super.onDraw(canvas); 172 173 final Drawable checkMarkDrawable = mCheckMarkDrawable; 174 if (checkMarkDrawable != null) { 175 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; 176 final int height = checkMarkDrawable.getIntrinsicHeight(); 177 178 int y = 0; 179 180 switch (verticalGravity) { 181 case Gravity.BOTTOM: 182 y = getHeight() - height; 183 break; 184 case Gravity.CENTER_VERTICAL: 185 y = (getHeight() - height) / 2; 186 break; 187 } 188 189 int right = getWidth(); 190 checkMarkDrawable.setBounds( 191 right - mPaddingRight, 192 y, 193 right - mPaddingRight + mCheckMarkWidth, 194 y + height); 195 checkMarkDrawable.draw(canvas); 196 } 197 } 198 199 @Override 200 protected int[] onCreateDrawableState(int extraSpace) { 201 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 202 if (isChecked()) { 203 mergeDrawableStates(drawableState, CHECKED_STATE_SET); 204 } 205 return drawableState; 206 } 207 208 @Override 209 protected void drawableStateChanged() { 210 super.drawableStateChanged(); 211 212 if (mCheckMarkDrawable != null) { 213 int[] myDrawableState = getDrawableState(); 214 215 // Set the state of the Drawable 216 mCheckMarkDrawable.setState(myDrawableState); 217 218 invalidate(); 219 } 220 } 221 222 @Override 223 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 224 super.onInitializeAccessibilityEvent(event); 225 event.setClassName(CheckedTextView.class.getName()); 226 event.setChecked(mChecked); 227 } 228 229 @Override 230 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 231 super.onInitializeAccessibilityNodeInfo(info); 232 info.setClassName(CheckedTextView.class.getName()); 233 info.setCheckable(true); 234 info.setChecked(mChecked); 235 } 236} 237