blob: aa9062b1f57f60828f11656cb25946d769e4a7fe [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;
svetoslavganov75986cf2009-05-14 22:28:01 -070027import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29
30/**
31 * An extension to TextView that supports the {@link android.widget.Checkable} interface.
32 * This is useful when used in a {@link android.widget.ListView ListView} where the it's
33 * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to
34 * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.
35 *
36 */
Romain Guy8b5e7c02009-04-29 11:48:22 -070037public class CheckedTextView extends TextView implements Checkable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 private boolean mChecked;
39 private int mCheckMarkResource;
40 private Drawable mCheckMarkDrawable;
41 private int mBasePaddingRight;
42 private int mCheckMarkWidth;
43
44 private static final int[] CHECKED_STATE_SET = {
45 R.attr.state_checked
46 };
47
48 public CheckedTextView(Context context) {
49 this(context, null);
50 }
51
52 public CheckedTextView(Context context, AttributeSet attrs) {
53 this(context, attrs, 0);
54 }
55
56 public CheckedTextView(Context context, AttributeSet attrs, int defStyle) {
57 super(context, attrs, defStyle);
58
59 TypedArray a = context.obtainStyledAttributes(attrs,
60 R.styleable.CheckedTextView, defStyle, 0);
61
62 Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
63 if (d != null) {
64 setCheckMarkDrawable(d);
65 }
66
67 boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
68 setChecked(checked);
69
70 a.recycle();
71 }
72
73 public void toggle() {
74 setChecked(!mChecked);
75 }
76
77 public boolean isChecked() {
78 return mChecked;
79 }
80
81 /**
82 * <p>Changes the checked state of this text view.</p>
83 *
84 * @param checked true to check the text, false to uncheck it
85 */
86 public void setChecked(boolean checked) {
87 if (mChecked != checked) {
88 mChecked = checked;
89 refreshDrawableState();
90 }
91 }
92
93
94 /**
95 * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn
96 * when {@link #isChecked()} is true.
97 *
98 * @param resid The Drawable to use for the checkmark.
99 */
100 public void setCheckMarkDrawable(int resid) {
101 if (resid != 0 && resid == mCheckMarkResource) {
102 return;
103 }
104
105 mCheckMarkResource = resid;
106
107 Drawable d = null;
108 if (mCheckMarkResource != 0) {
109 d = getResources().getDrawable(mCheckMarkResource);
110 }
111 setCheckMarkDrawable(d);
112 }
113
114 /**
115 * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.
116 *
117 * @param d The Drawable to use for the checkmark.
118 */
119 public void setCheckMarkDrawable(Drawable d) {
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400120 if (mCheckMarkDrawable != null) {
121 mCheckMarkDrawable.setCallback(null);
122 unscheduleDrawable(mCheckMarkDrawable);
123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 if (d != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 d.setCallback(this);
126 d.setVisible(getVisibility() == VISIBLE, false);
127 d.setState(CHECKED_STATE_SET);
128 setMinHeight(d.getIntrinsicHeight());
129
130 mCheckMarkWidth = d.getIntrinsicWidth();
131 mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
132 d.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 } else {
134 mPaddingRight = mBasePaddingRight;
135 }
Leon Scrogginsa8da1732009-10-19 19:04:30 -0400136 mCheckMarkDrawable = d;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 requestLayout();
138 }
139
140 @Override
141 public void setPadding(int left, int top, int right, int bottom) {
142 super.setPadding(left, top, right, bottom);
143 mBasePaddingRight = mPaddingRight;
144 }
145
146 @Override
147 protected void onDraw(Canvas canvas) {
148 super.onDraw(canvas);
149
150 final Drawable checkMarkDrawable = mCheckMarkDrawable;
151 if (checkMarkDrawable != null) {
152 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
153 final int height = checkMarkDrawable.getIntrinsicHeight();
154
155 int y = 0;
156
157 switch (verticalGravity) {
158 case Gravity.BOTTOM:
159 y = getHeight() - height;
160 break;
161 case Gravity.CENTER_VERTICAL:
162 y = (getHeight() - height) / 2;
163 break;
164 }
165
166 int right = getWidth();
167 checkMarkDrawable.setBounds(
168 right - mCheckMarkWidth - mBasePaddingRight,
169 y,
170 right - mBasePaddingRight,
171 y + height);
172 checkMarkDrawable.draw(canvas);
173 }
174 }
175
176 @Override
177 protected int[] onCreateDrawableState(int extraSpace) {
178 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
179 if (isChecked()) {
180 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
181 }
182 return drawableState;
183 }
184
185 @Override
186 protected void drawableStateChanged() {
187 super.drawableStateChanged();
188
189 if (mCheckMarkDrawable != null) {
190 int[] myDrawableState = getDrawableState();
191
192 // Set the state of the Drawable
193 mCheckMarkDrawable.setState(myDrawableState);
194
195 invalidate();
196 }
197 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700198
199 @Override
200 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
201 boolean populated = super.dispatchPopulateAccessibilityEvent(event);
202 if (!populated) {
203 event.setChecked(mChecked);
204 }
205 return populated;
206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207}