2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.widget;
19 import com.android.internal.R;
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;
32 * An extension to TextView that supports the {@link android.widget.Checkable} interface.
33 * This is useful when used in a {@link android.widget.ListView ListView} where the it's
34 * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to
35 * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.
38 public class CheckedTextView extends TextView implements Checkable {
39 private boolean mChecked;
40 private int mCheckMarkResource;
41 private Drawable mCheckMarkDrawable;
42 private int mBasePaddingRight;
43 private int mCheckMarkWidth;
45 private static final int[] CHECKED_STATE_SET = {
49 public CheckedTextView(Context context) {
53 public CheckedTextView(Context context, AttributeSet attrs) {
54 this(context, attrs, 0);
57 public CheckedTextView(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
60 TypedArray a = context.obtainStyledAttributes(attrs,
61 R.styleable.CheckedTextView, defStyle, 0);
63 Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
65 setCheckMarkDrawable(d);
68 boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
74 public void toggle() {
75 setChecked(!mChecked);
78 @ViewDebug.ExportedProperty
79 public boolean isChecked() {
84 * <p>Changes the checked state of this text view.</p>
86 * @param checked true to check the text, false to uncheck it
88 public void setChecked(boolean checked) {
89 if (mChecked != checked) {
91 refreshDrawableState();
97 * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn
98 * when {@link #isChecked()} is true.
100 * @param resid The Drawable to use for the checkmark.
102 public void setCheckMarkDrawable(int resid) {
103 if (resid != 0 && resid == mCheckMarkResource) {
107 mCheckMarkResource = resid;
110 if (mCheckMarkResource != 0) {
111 d = getResources().getDrawable(mCheckMarkResource);
113 setCheckMarkDrawable(d);
117 * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.
119 * @param d The Drawable to use for the checkmark.
121 public void setCheckMarkDrawable(Drawable d) {
122 if (mCheckMarkDrawable != null) {
123 mCheckMarkDrawable.setCallback(null);
124 unscheduleDrawable(mCheckMarkDrawable);
128 d.setVisible(getVisibility() == VISIBLE, false);
129 d.setState(CHECKED_STATE_SET);
130 setMinHeight(d.getIntrinsicHeight());
132 mCheckMarkWidth = d.getIntrinsicWidth();
133 mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
134 d.setState(getDrawableState());
136 mPaddingRight = mBasePaddingRight;
138 mCheckMarkDrawable = d;
143 public void setPadding(int left, int top, int right, int bottom) {
144 super.setPadding(left, top, right, bottom);
145 mBasePaddingRight = mPaddingRight;
149 protected void onDraw(Canvas canvas) {
150 super.onDraw(canvas);
152 final Drawable checkMarkDrawable = mCheckMarkDrawable;
153 if (checkMarkDrawable != null) {
154 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
155 final int height = checkMarkDrawable.getIntrinsicHeight();
159 switch (verticalGravity) {
161 y = getHeight() - height;
163 case Gravity.CENTER_VERTICAL:
164 y = (getHeight() - height) / 2;
168 int right = getWidth();
169 checkMarkDrawable.setBounds(
170 right - mCheckMarkWidth - mBasePaddingRight,
172 right - mBasePaddingRight,
174 checkMarkDrawable.draw(canvas);
179 protected int[] onCreateDrawableState(int extraSpace) {
180 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
182 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
184 return drawableState;
188 protected void drawableStateChanged() {
189 super.drawableStateChanged();
191 if (mCheckMarkDrawable != null) {
192 int[] myDrawableState = getDrawableState();
194 // Set the state of the Drawable
195 mCheckMarkDrawable.setState(myDrawableState);
202 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
203 boolean populated = super.dispatchPopulateAccessibilityEvent(event);
205 event.setChecked(mChecked);