blob: 2410eb2349f613a43fa1375c45fd6e36695adfe1 [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
19import com.android.internal.R;
20
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.Canvas;
24import android.graphics.drawable.Drawable;
25import android.os.Parcel;
26import android.os.Parcelable;
27import android.util.AttributeSet;
28import android.view.Gravity;
Steve Zeigler7a367882010-02-23 16:39:08 -080029import android.view.ViewDebug;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070030import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070031import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33/**
34 * <p>
35 * A button with two states, checked and unchecked. When the button is pressed
36 * or clicked, the state changes automatically.
37 * </p>
38 *
39 * <p><strong>XML attributes</strong></p>
40 * <p>
41 * See {@link android.R.styleable#CompoundButton
42 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
43 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
44 * android.R.styleable#View View Attributes}
45 * </p>
46 */
47public abstract class CompoundButton extends Button implements Checkable {
48 private boolean mChecked;
49 private int mButtonResource;
50 private boolean mBroadcasting;
51 private Drawable mButtonDrawable;
52 private OnCheckedChangeListener mOnCheckedChangeListener;
53 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
54
55 private static final int[] CHECKED_STATE_SET = {
56 R.attr.state_checked
57 };
58
59 public CompoundButton(Context context) {
60 this(context, null);
61 }
62
63 public CompoundButton(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public CompoundButton(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69
70 TypedArray a =
71 context.obtainStyledAttributes(
72 attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);
73
74 Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
75 if (d != null) {
76 setButtonDrawable(d);
77 }
78
79 boolean checked = a
80 .getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);
81 setChecked(checked);
82
83 a.recycle();
84 }
85
86 public void toggle() {
87 setChecked(!mChecked);
88 }
89
90 @Override
91 public boolean performClick() {
92 /*
93 * XXX: These are tiny, need some surrounding 'expanded touch area',
94 * which will need to be implemented in Button if we only override
95 * performClick()
96 */
97
98 /* When clicked, toggle the state */
99 toggle();
100 return super.performClick();
101 }
102
Steve Zeigler7a367882010-02-23 16:39:08 -0800103 @ViewDebug.ExportedProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 public boolean isChecked() {
105 return mChecked;
106 }
107
108 /**
109 * <p>Changes the checked state of this button.</p>
110 *
111 * @param checked true to check the button, false to uncheck it
112 */
113 public void setChecked(boolean checked) {
114 if (mChecked != checked) {
115 mChecked = checked;
116 refreshDrawableState();
117
118 // Avoid infinite recursions if setChecked() is called from a listener
119 if (mBroadcasting) {
120 return;
121 }
122
123 mBroadcasting = true;
124 if (mOnCheckedChangeListener != null) {
125 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
126 }
127 if (mOnCheckedChangeWidgetListener != null) {
128 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
129 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 mBroadcasting = false;
132 }
133 }
134
135 /**
136 * Register a callback to be invoked when the checked state of this button
137 * changes.
138 *
139 * @param listener the callback to call on checked state change
140 */
141 public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
142 mOnCheckedChangeListener = listener;
143 }
144
145 /**
146 * Register a callback to be invoked when the checked state of this button
147 * changes. This callback is used for internal purpose only.
148 *
149 * @param listener the callback to call on checked state change
150 * @hide
151 */
152 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
153 mOnCheckedChangeWidgetListener = listener;
154 }
155
156 /**
157 * Interface definition for a callback to be invoked when the checked state
158 * of a compound button changed.
159 */
160 public static interface OnCheckedChangeListener {
161 /**
162 * Called when the checked state of a compound button has changed.
163 *
164 * @param buttonView The compound button view whose state has changed.
165 * @param isChecked The new checked state of buttonView.
166 */
167 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
168 }
169
170 /**
171 * Set the background to a given Drawable, identified by its resource id.
172 *
173 * @param resid the resource id of the drawable to use as the background
174 */
175 public void setButtonDrawable(int resid) {
176 if (resid != 0 && resid == mButtonResource) {
177 return;
178 }
179
180 mButtonResource = resid;
181
182 Drawable d = null;
183 if (mButtonResource != 0) {
184 d = getResources().getDrawable(mButtonResource);
185 }
186 setButtonDrawable(d);
187 }
188
189 /**
190 * Set the background to a given Drawable
191 *
192 * @param d The Drawable to use as the background
193 */
194 public void setButtonDrawable(Drawable d) {
195 if (d != null) {
196 if (mButtonDrawable != null) {
197 mButtonDrawable.setCallback(null);
198 unscheduleDrawable(mButtonDrawable);
199 }
200 d.setCallback(this);
201 d.setState(getDrawableState());
202 d.setVisible(getVisibility() == VISIBLE, false);
203 mButtonDrawable = d;
204 mButtonDrawable.setState(null);
205 setMinHeight(mButtonDrawable.getIntrinsicHeight());
206 }
207
208 refreshDrawableState();
209 }
210
211 @Override
Svetoslav Ganov30401322011-05-12 18:53:45 -0700212 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
213 super.onInitializeAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700214 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700215 }
216
217 @Override
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700218 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
219 super.onInitializeAccessibilityNodeInfo(info);
220 info.setChecked(mChecked);
221 }
222
223 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 protected void onDraw(Canvas canvas) {
225 super.onDraw(canvas);
226
227 final Drawable buttonDrawable = mButtonDrawable;
228 if (buttonDrawable != null) {
229 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
230 final int height = buttonDrawable.getIntrinsicHeight();
231
232 int y = 0;
233
234 switch (verticalGravity) {
235 case Gravity.BOTTOM:
236 y = getHeight() - height;
237 break;
238 case Gravity.CENTER_VERTICAL:
239 y = (getHeight() - height) / 2;
240 break;
241 }
242
243 buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);
244 buttonDrawable.draw(canvas);
245 }
246 }
247
248 @Override
249 protected int[] onCreateDrawableState(int extraSpace) {
250 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
251 if (isChecked()) {
252 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
253 }
254 return drawableState;
255 }
256
257 @Override
258 protected void drawableStateChanged() {
259 super.drawableStateChanged();
260
261 if (mButtonDrawable != null) {
262 int[] myDrawableState = getDrawableState();
263
264 // Set the state of the Drawable
265 mButtonDrawable.setState(myDrawableState);
266
267 invalidate();
268 }
269 }
270
271 @Override
272 protected boolean verifyDrawable(Drawable who) {
273 return super.verifyDrawable(who) || who == mButtonDrawable;
274 }
275
Dianne Hackborne2136772010-11-04 15:08:59 -0700276 @Override
277 public void jumpDrawablesToCurrentState() {
278 super.jumpDrawablesToCurrentState();
279 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
280 }
281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 static class SavedState extends BaseSavedState {
283 boolean checked;
284
285 /**
286 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
287 */
288 SavedState(Parcelable superState) {
289 super(superState);
290 }
291
292 /**
293 * Constructor called from {@link #CREATOR}
294 */
295 private SavedState(Parcel in) {
296 super(in);
297 checked = (Boolean)in.readValue(null);
298 }
299
300 @Override
301 public void writeToParcel(Parcel out, int flags) {
302 super.writeToParcel(out, flags);
303 out.writeValue(checked);
304 }
305
306 @Override
307 public String toString() {
308 return "CompoundButton.SavedState{"
309 + Integer.toHexString(System.identityHashCode(this))
310 + " checked=" + checked + "}";
311 }
312
313 public static final Parcelable.Creator<SavedState> CREATOR
314 = new Parcelable.Creator<SavedState>() {
315 public SavedState createFromParcel(Parcel in) {
316 return new SavedState(in);
317 }
318
319 public SavedState[] newArray(int size) {
320 return new SavedState[size];
321 }
322 };
323 }
324
325 @Override
326 public Parcelable onSaveInstanceState() {
327 // Force our ancestor class to save its state
328 setFreezesText(true);
329 Parcelable superState = super.onSaveInstanceState();
330
331 SavedState ss = new SavedState(superState);
332
333 ss.checked = isChecked();
334 return ss;
335 }
336
337 @Override
338 public void onRestoreInstanceState(Parcelable state) {
339 SavedState ss = (SavedState) state;
340
341 super.onRestoreInstanceState(ss.getSuperState());
342 setChecked(ss.checked);
343 requestLayout();
344 }
345}