blob: 17c6f1d1ff526ac50b52eabdeaea36ca1aba190a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
svetoslavganov75986cf2009-05-14 22:28:01 -070019import com.android.internal.R;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import 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;
Steve Zeigler7a367882010-02-23 16:39:08 -080027import android.view.ViewDebug;
svetoslavganov75986cf2009-05-14 22:28:01 -070028import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -070029import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
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 Debunne2fb40282012-05-01 12:07:06 -070038 * @attr ref android.R.styleable#CheckedTextView_checked
39 * @attr ref android.R.styleable#CheckedTextView_checkMark
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 */
Romain Guy8b5e7c02009-04-29 11:48:22 -070041public class CheckedTextView extends TextView implements Checkable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 private boolean mChecked;
43 private int mCheckMarkResource;
44 private Drawable mCheckMarkDrawable;
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070045 private int mBasePadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 private int mCheckMarkWidth;
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070047 private boolean mNeedRequestlayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -070049 private int initialPaddingLeft = -1;
50 private int initialPaddingRight = -1;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 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 Zeigler7a367882010-02-23 16:39:08 -080084
85 @ViewDebug.ExportedProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 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 Ganovc406be92012-05-11 16:12:32 -070099 notifyAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 }
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 Meglio343e1132012-09-28 18:01:17 -0700107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * @param resid The Drawable to use for the checkmark.
Gilles Debunne2fb40282012-05-01 12:07:06 -0700109 *
110 * @see #setCheckMarkDrawable(Drawable)
111 * @see #getCheckMarkDrawable()
112 *
113 * @attr ref android.R.styleable#CheckedTextView_checkMark
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
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 Debunne2fb40282012-05-01 12:07:06 -0700133 *
134 * @see #setCheckMarkDrawable(int)
135 * @see #getCheckMarkDrawable()
136 *
137 * @attr ref android.R.styleable#CheckedTextView_checkMark
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 */
139 public void setCheckMarkDrawable(Drawable d) {
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400140 if (mCheckMarkDrawable != null) {
141 mCheckMarkDrawable.setCallback(null);
142 unscheduleDrawable(mCheckMarkDrawable);
143 }
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700144 mNeedRequestlayout = (d != mCheckMarkDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 if (d != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 d.setCallback(this);
147 d.setVisible(getVisibility() == VISIBLE, false);
148 d.setState(CHECKED_STATE_SET);
149 setMinHeight(d.getIntrinsicHeight());
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 mCheckMarkWidth = d.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 d.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 } else {
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700154 mCheckMarkWidth = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400156 mCheckMarkDrawable = d;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700157 // Do padding resolution. This will call internalSetPadding() and do a requestLayout() if needed.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700158 resolvePadding();
159 }
160
Gilles Debunne2fb40282012-05-01 12:07:06 -0700161 /**
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 Meglio23c89fd2012-08-13 12:17:42 -0700175 /**
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 Meglio0dc96462012-08-24 12:25:28 -0700181 setBasePadding(isLayoutRtl());
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700182 initialPaddingLeft = mPaddingLeft;
183 initialPaddingRight = mPaddingRight;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700184 }
185
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700186 @Override
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700187 public void onRtlPropertiesChanged(int layoutDirection) {
188 super.onRtlPropertiesChanged(layoutDirection);
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700189 updatePadding();
190 }
191
192 private void updatePadding() {
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700193 int newPadding = (mCheckMarkDrawable != null) ?
194 mCheckMarkWidth + mBasePadding : mBasePadding;
195 mNeedRequestlayout |= (mPaddingRight != newPadding);
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700196 if (isLayoutRtl()) {
197 mPaddingLeft = newPadding;
198 } else {
199 mPaddingRight = newPadding;
200 }
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700201 if (mNeedRequestlayout) {
202 requestLayout();
203 mNeedRequestlayout = false;
204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 @Override
208 public void setPadding(int left, int top, int right, int bottom) {
209 super.setPadding(left, top, right, bottom);
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700210 setBasePadding(isLayoutRtl());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212
213 @Override
Fabrice Di Megliobf923eb2012-03-07 16:20:22 -0800214 public void setPaddingRelative(int start, int top, int end, int bottom) {
215 super.setPaddingRelative(start, top, end, bottom);
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700216 setBasePadding(isLayoutRtl());
217 }
218
219 private void setBasePadding(boolean isLayoutRtl) {
220 if (isLayoutRtl) {
221 mBasePadding = mPaddingLeft;
222 } else {
223 mBasePadding = mPaddingRight;
224 }
Fabrice Di Megliobf923eb2012-03-07 16:20:22 -0800225 }
226
227 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 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 Meglioe2386c12012-07-26 15:17:16 -0700247 final boolean isLayoutRtl = isLayoutRtl();
248 final int width = getWidth();
249 final int top = y;
250 final int bottom = top + height;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700251 final int left;
252 final int right;
253 if (isLayoutRtl) {
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700254 left = mBasePadding;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700255 right = left + mCheckMarkWidth;
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700256 } else {
257 right = width - mBasePadding;
258 left = right - mCheckMarkWidth;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700259 }
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700260 checkMarkDrawable.setBounds( left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 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 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700287
288 @Override
Svetoslav Ganov30401322011-05-12 18:53:45 -0700289 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
290 super.onInitializeAccessibilityEvent(event);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800291 event.setClassName(CheckedTextView.class.getName());
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700292 event.setChecked(mChecked);
svetoslavganov75986cf2009-05-14 22:28:01 -0700293 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700294
295 @Override
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -0700296 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
297 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800298 info.setClassName(CheckedTextView.class.getName());
Svetoslav Ganovc85015c2012-04-30 11:37:43 -0700299 info.setCheckable(true);
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -0700300 info.setChecked(mChecked);
301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302}