blob: 40747f0ca8c38aa2bcc2d2c64d46adc02afac47d [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
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 Megliob023a582012-09-30 15:31:06 -070058 this(context, attrs, R.attr.checkedTextViewStyle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
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 Zeigler7a367882010-02-23 16:39:08 -080081
82 @ViewDebug.ExportedProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 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();
Svetoslav6254f482013-06-04 17:22:14 -070096 notifyViewAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
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.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * @param resid The Drawable to use for the checkmark.
Gilles Debunne2fb40282012-05-01 12:07:06 -0700106 *
107 * @see #setCheckMarkDrawable(Drawable)
108 * @see #getCheckMarkDrawable()
109 *
110 * @attr ref android.R.styleable#CheckedTextView_checkMark
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 */
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.
Gilles Debunne2fb40282012-05-01 12:07:06 -0700130 *
131 * @see #setCheckMarkDrawable(int)
132 * @see #getCheckMarkDrawable()
133 *
134 * @attr ref android.R.styleable#CheckedTextView_checkMark
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 */
136 public void setCheckMarkDrawable(Drawable d) {
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400137 if (mCheckMarkDrawable != null) {
138 mCheckMarkDrawable.setCallback(null);
139 unscheduleDrawable(mCheckMarkDrawable);
140 }
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700141 mNeedRequestlayout = (d != mCheckMarkDrawable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 if (d != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 d.setCallback(this);
144 d.setVisible(getVisibility() == VISIBLE, false);
145 d.setState(CHECKED_STATE_SET);
146 setMinHeight(d.getIntrinsicHeight());
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 mCheckMarkWidth = d.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 d.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 } else {
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700151 mCheckMarkWidth = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400153 mCheckMarkDrawable = d;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700154 // Do padding resolution. This will call internalSetPadding() and do a requestLayout() if needed.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700155 resolvePadding();
156 }
157
Gilles Debunne2fb40282012-05-01 12:07:06 -0700158 /**
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
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700172 /**
173 * @hide
174 */
175 @Override
176 protected void internalSetPadding(int left, int top, int right, int bottom) {
177 super.internalSetPadding(left, top, right, bottom);
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700178 setBasePadding(isLayoutRtl());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700179 }
180
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700181 @Override
Fabrice Di Meglio343e1132012-09-28 18:01:17 -0700182 public void onRtlPropertiesChanged(int layoutDirection) {
183 super.onRtlPropertiesChanged(layoutDirection);
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700184 updatePadding();
185 }
186
187 private void updatePadding() {
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -0700188 resetPaddingToInitialValues();
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700189 int newPadding = (mCheckMarkDrawable != null) ?
190 mCheckMarkWidth + mBasePadding : mBasePadding;
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700191 if (isLayoutRtl()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -0700192 mNeedRequestlayout |= (mPaddingLeft != newPadding);
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700193 mPaddingLeft = newPadding;
194 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -0700195 mNeedRequestlayout |= (mPaddingRight != newPadding);
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700196 mPaddingRight = newPadding;
197 }
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -0700198 if (mNeedRequestlayout) {
199 requestLayout();
200 mNeedRequestlayout = false;
201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
Fabrice Di Meglio15bbde42012-09-28 15:49:38 -0700203
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700204 private void setBasePadding(boolean isLayoutRtl) {
205 if (isLayoutRtl) {
206 mBasePadding = mPaddingLeft;
207 } else {
208 mBasePadding = mPaddingRight;
209 }
Fabrice Di Megliobf923eb2012-03-07 16:20:22 -0800210 }
211
212 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 protected void onDraw(Canvas canvas) {
214 super.onDraw(canvas);
215
216 final Drawable checkMarkDrawable = mCheckMarkDrawable;
217 if (checkMarkDrawable != null) {
218 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
219 final int height = checkMarkDrawable.getIntrinsicHeight();
220
221 int y = 0;
222
223 switch (verticalGravity) {
224 case Gravity.BOTTOM:
225 y = getHeight() - height;
226 break;
227 case Gravity.CENTER_VERTICAL:
228 y = (getHeight() - height) / 2;
229 break;
230 }
231
Fabrice Di Meglioe2386c12012-07-26 15:17:16 -0700232 final boolean isLayoutRtl = isLayoutRtl();
233 final int width = getWidth();
234 final int top = y;
235 final int bottom = top + height;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700236 final int left;
237 final int right;
238 if (isLayoutRtl) {
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700239 left = mBasePadding;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700240 right = left + mCheckMarkWidth;
Fabrice Di Meglio0dc96462012-08-24 12:25:28 -0700241 } else {
242 right = width - mBasePadding;
243 left = right - mCheckMarkWidth;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -0700244 }
Jorn Jacobsson78cdc552013-01-31 17:52:20 +0100245 checkMarkDrawable.setBounds(mScrollX + left, top, mScrollX + right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 checkMarkDrawable.draw(canvas);
247 }
248 }
249
250 @Override
251 protected int[] onCreateDrawableState(int extraSpace) {
252 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
253 if (isChecked()) {
254 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
255 }
256 return drawableState;
257 }
258
259 @Override
260 protected void drawableStateChanged() {
261 super.drawableStateChanged();
262
263 if (mCheckMarkDrawable != null) {
264 int[] myDrawableState = getDrawableState();
265
266 // Set the state of the Drawable
267 mCheckMarkDrawable.setState(myDrawableState);
268
269 invalidate();
270 }
271 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700272
273 @Override
Svetoslav Ganov30401322011-05-12 18:53:45 -0700274 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
275 super.onInitializeAccessibilityEvent(event);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800276 event.setClassName(CheckedTextView.class.getName());
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700277 event.setChecked(mChecked);
svetoslavganov75986cf2009-05-14 22:28:01 -0700278 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700279
280 @Override
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -0700281 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
282 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800283 info.setClassName(CheckedTextView.class.getName());
Svetoslav Ganovc85015c2012-04-30 11:37:43 -0700284 info.setCheckable(true);
Svetoslav Ganov34ffaab2011-09-12 16:19:32 -0700285 info.setChecked(mChecked);
286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287}