blob: 05a8dc825e124de072de31e96f81798e2d4238a5 [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();
Svetoslav6254f482013-06-04 17:22:14 -0700117 notifyViewAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 // Avoid infinite recursions if setChecked() is called from a listener
120 if (mBroadcasting) {
121 return;
122 }
123
124 mBroadcasting = true;
125 if (mOnCheckedChangeListener != null) {
126 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
127 }
128 if (mOnCheckedChangeWidgetListener != null) {
129 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
130 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mBroadcasting = false;
133 }
134 }
135
136 /**
137 * Register a callback to be invoked when the checked state of this button
138 * changes.
139 *
140 * @param listener the callback to call on checked state change
141 */
142 public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
143 mOnCheckedChangeListener = listener;
144 }
145
146 /**
147 * Register a callback to be invoked when the checked state of this button
148 * changes. This callback is used for internal purpose only.
149 *
150 * @param listener the callback to call on checked state change
151 * @hide
152 */
153 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
154 mOnCheckedChangeWidgetListener = listener;
155 }
156
157 /**
158 * Interface definition for a callback to be invoked when the checked state
159 * of a compound button changed.
160 */
161 public static interface OnCheckedChangeListener {
162 /**
163 * Called when the checked state of a compound button has changed.
164 *
165 * @param buttonView The compound button view whose state has changed.
166 * @param isChecked The new checked state of buttonView.
167 */
168 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
169 }
170
171 /**
172 * Set the background to a given Drawable, identified by its resource id.
173 *
174 * @param resid the resource id of the drawable to use as the background
175 */
176 public void setButtonDrawable(int resid) {
177 if (resid != 0 && resid == mButtonResource) {
178 return;
179 }
180
181 mButtonResource = resid;
182
183 Drawable d = null;
184 if (mButtonResource != 0) {
185 d = getResources().getDrawable(mButtonResource);
186 }
187 setButtonDrawable(d);
188 }
189
190 /**
191 * Set the background to a given Drawable
192 *
193 * @param d The Drawable to use as the background
194 */
195 public void setButtonDrawable(Drawable d) {
196 if (d != null) {
197 if (mButtonDrawable != null) {
198 mButtonDrawable.setCallback(null);
199 unscheduleDrawable(mButtonDrawable);
200 }
201 d.setCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 d.setVisible(getVisibility() == VISIBLE, false);
203 mButtonDrawable = d;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 setMinHeight(mButtonDrawable.getIntrinsicHeight());
205 }
206
207 refreshDrawableState();
208 }
209
210 @Override
Svetoslav Ganov30401322011-05-12 18:53:45 -0700211 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
212 super.onInitializeAccessibilityEvent(event);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800213 event.setClassName(CompoundButton.class.getName());
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);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800220 info.setClassName(CompoundButton.class.getName());
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700221 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700222 info.setChecked(mChecked);
223 }
224
225 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700226 public int getCompoundPaddingLeft() {
227 int padding = super.getCompoundPaddingLeft();
228 if (!isLayoutRtl()) {
229 final Drawable buttonDrawable = mButtonDrawable;
230 if (buttonDrawable != null) {
231 padding += buttonDrawable.getIntrinsicWidth();
232 }
233 }
234 return padding;
235 }
236
237 @Override
238 public int getCompoundPaddingRight() {
239 int padding = super.getCompoundPaddingRight();
240 if (isLayoutRtl()) {
241 final Drawable buttonDrawable = mButtonDrawable;
242 if (buttonDrawable != null) {
243 padding += buttonDrawable.getIntrinsicWidth();
244 }
245 }
246 return padding;
247 }
248
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800249 /**
250 * @hide
251 */
252 @Override
253 public int getHorizontalOffsetForDrawables() {
254 final Drawable buttonDrawable = mButtonDrawable;
255 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
256 }
257
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 protected void onDraw(Canvas canvas) {
260 super.onDraw(canvas);
261
262 final Drawable buttonDrawable = mButtonDrawable;
263 if (buttonDrawable != null) {
264 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700265 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
266 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700268 int top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 switch (verticalGravity) {
270 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700271 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 break;
273 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700274 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 break;
276 }
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700277 int bottom = top + drawableHeight;
278 int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
279 int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700281 buttonDrawable.setBounds(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 buttonDrawable.draw(canvas);
283 }
284 }
285
286 @Override
287 protected int[] onCreateDrawableState(int extraSpace) {
288 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
289 if (isChecked()) {
290 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
291 }
292 return drawableState;
293 }
294
295 @Override
296 protected void drawableStateChanged() {
297 super.drawableStateChanged();
298
299 if (mButtonDrawable != null) {
300 int[] myDrawableState = getDrawableState();
301
302 // Set the state of the Drawable
303 mButtonDrawable.setState(myDrawableState);
304
305 invalidate();
306 }
307 }
308
309 @Override
310 protected boolean verifyDrawable(Drawable who) {
311 return super.verifyDrawable(who) || who == mButtonDrawable;
312 }
313
Dianne Hackborne2136772010-11-04 15:08:59 -0700314 @Override
315 public void jumpDrawablesToCurrentState() {
316 super.jumpDrawablesToCurrentState();
317 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
318 }
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 static class SavedState extends BaseSavedState {
321 boolean checked;
322
323 /**
324 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
325 */
326 SavedState(Parcelable superState) {
327 super(superState);
328 }
329
330 /**
331 * Constructor called from {@link #CREATOR}
332 */
333 private SavedState(Parcel in) {
334 super(in);
335 checked = (Boolean)in.readValue(null);
336 }
337
338 @Override
339 public void writeToParcel(Parcel out, int flags) {
340 super.writeToParcel(out, flags);
341 out.writeValue(checked);
342 }
343
344 @Override
345 public String toString() {
346 return "CompoundButton.SavedState{"
347 + Integer.toHexString(System.identityHashCode(this))
348 + " checked=" + checked + "}";
349 }
350
351 public static final Parcelable.Creator<SavedState> CREATOR
352 = new Parcelable.Creator<SavedState>() {
353 public SavedState createFromParcel(Parcel in) {
354 return new SavedState(in);
355 }
356
357 public SavedState[] newArray(int size) {
358 return new SavedState[size];
359 }
360 };
361 }
362
363 @Override
364 public Parcelable onSaveInstanceState() {
365 // Force our ancestor class to save its state
366 setFreezesText(true);
367 Parcelable superState = super.onSaveInstanceState();
368
369 SavedState ss = new SavedState(superState);
370
371 ss.checked = isChecked();
372 return ss;
373 }
374
375 @Override
376 public void onRestoreInstanceState(Parcelable state) {
377 SavedState ss = (SavedState) state;
378
379 super.onRestoreInstanceState(ss.getSuperState());
380 setChecked(ss.checked);
381 requestLayout();
382 }
383}