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 19 import com.android.internal.R; 20 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; 27 import android.view.ViewDebug; 28 import android.view.accessibility.AccessibilityEvent; 29 import 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 */ 39 public 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 CheckedTextView(Context context)51 public CheckedTextView(Context context) { 52 this(context, null); 53 } 54 CheckedTextView(Context context, AttributeSet attrs)55 public CheckedTextView(Context context, AttributeSet attrs) { 56 this(context, attrs, 0); 57 } 58 CheckedTextView(Context context, AttributeSet attrs, int defStyle)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 toggle()76 public void toggle() { 77 setChecked(!mChecked); 78 } 79 80 @ViewDebug.ExportedProperty isChecked()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 */ setChecked(boolean checked)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 */ setCheckMarkDrawable(int resid)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 */ setCheckMarkDrawable(Drawable d)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 /** 146 * @hide 147 */ 148 @Override resolvePadding()149 protected void resolvePadding() { 150 super.resolvePadding(); 151 int newPadding = (mCheckMarkDrawable != null) ? 152 mCheckMarkWidth + mBasePadding : mBasePadding; 153 mNeedRequestlayout |= (mPaddingRight != newPadding); 154 mPaddingRight = newPadding; 155 if (mNeedRequestlayout) { 156 requestLayout(); 157 mNeedRequestlayout = false; 158 } 159 } 160 161 @Override setPadding(int left, int top, int right, int bottom)162 public void setPadding(int left, int top, int right, int bottom) { 163 super.setPadding(left, top, right, bottom); 164 mBasePadding = mPaddingRight; 165 } 166 167 @Override onDraw(Canvas canvas)168 protected void onDraw(Canvas canvas) { 169 super.onDraw(canvas); 170 171 final Drawable checkMarkDrawable = mCheckMarkDrawable; 172 if (checkMarkDrawable != null) { 173 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; 174 final int height = checkMarkDrawable.getIntrinsicHeight(); 175 176 int y = 0; 177 178 switch (verticalGravity) { 179 case Gravity.BOTTOM: 180 y = getHeight() - height; 181 break; 182 case Gravity.CENTER_VERTICAL: 183 y = (getHeight() - height) / 2; 184 break; 185 } 186 187 int right = getWidth(); 188 checkMarkDrawable.setBounds( 189 right - mPaddingRight, 190 y, 191 right - mPaddingRight + mCheckMarkWidth, 192 y + height); 193 checkMarkDrawable.draw(canvas); 194 } 195 } 196 197 @Override onCreateDrawableState(int extraSpace)198 protected int[] onCreateDrawableState(int extraSpace) { 199 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 200 if (isChecked()) { 201 mergeDrawableStates(drawableState, CHECKED_STATE_SET); 202 } 203 return drawableState; 204 } 205 206 @Override drawableStateChanged()207 protected void drawableStateChanged() { 208 super.drawableStateChanged(); 209 210 if (mCheckMarkDrawable != null) { 211 int[] myDrawableState = getDrawableState(); 212 213 // Set the state of the Drawable 214 mCheckMarkDrawable.setState(myDrawableState); 215 216 invalidate(); 217 } 218 } 219 220 @Override onInitializeAccessibilityEvent(AccessibilityEvent event)221 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 222 super.onInitializeAccessibilityEvent(event); 223 event.setChecked(mChecked); 224 } 225 226 @Override onPopulateAccessibilityEvent(AccessibilityEvent event)227 public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 228 super.onPopulateAccessibilityEvent(event); 229 if (isChecked()) { 230 event.getText().add(mContext.getString(R.string.radiobutton_selected)); 231 } else { 232 event.getText().add(mContext.getString(R.string.radiobutton_not_selected)); 233 } 234 } 235 236 @Override onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)237 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 238 super.onInitializeAccessibilityNodeInfo(info); 239 info.setChecked(mChecked); 240 } 241 } 242