blob: c1d8cb30d011334a0efeba246ba615978f744ecd [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
Alan Viverette91174362014-06-17 14:51:45 -070019import android.annotation.Nullable;
20import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.R;
22
23import android.content.Context;
Alan Viverette91174362014-06-17 14:51:45 -070024import android.content.res.ColorStateList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.res.TypedArray;
26import android.graphics.Canvas;
27import android.graphics.drawable.Drawable;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
31import android.view.Gravity;
Steve Zeigler7a367882010-02-23 16:39:08 -080032import android.view.ViewDebug;
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070033import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov13774d22011-06-15 15:29:51 -070034import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36/**
37 * <p>
38 * A button with two states, checked and unchecked. When the button is pressed
39 * or clicked, the state changes automatically.
40 * </p>
41 *
42 * <p><strong>XML attributes</strong></p>
43 * <p>
44 * See {@link android.R.styleable#CompoundButton
45 * CompoundButton Attributes}, {@link android.R.styleable#Button Button
46 * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
47 * android.R.styleable#View View Attributes}
48 * </p>
49 */
50public abstract class CompoundButton extends Button implements Checkable {
51 private boolean mChecked;
52 private int mButtonResource;
53 private boolean mBroadcasting;
Alan Viverette91174362014-06-17 14:51:45 -070054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 private Drawable mButtonDrawable;
Alan Viverettea4264452014-07-28 16:02:55 -070056 private ColorStateList mButtonTintList = null;
Alan Viverette91174362014-06-17 14:51:45 -070057 private PorterDuff.Mode mButtonTintMode = PorterDuff.Mode.SRC_ATOP;
58 private boolean mHasButtonTint = false;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 private OnCheckedChangeListener mOnCheckedChangeListener;
61 private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
62
63 private static final int[] CHECKED_STATE_SET = {
64 R.attr.state_checked
65 };
66
67 public CompoundButton(Context context) {
68 this(context, null);
69 }
70
71 public CompoundButton(Context context, AttributeSet attrs) {
72 this(context, attrs, 0);
73 }
74
Alan Viverette617feb92013-09-09 18:09:13 -070075 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
76 this(context, attrs, defStyleAttr, 0);
77 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Alan Viverette617feb92013-09-09 18:09:13 -070079 public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
80 super(context, attrs, defStyleAttr, defStyleRes);
81
82 final TypedArray a = context.obtainStyledAttributes(
83 attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Alan Viverette91174362014-06-17 14:51:45 -070085 final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 if (d != null) {
87 setButtonDrawable(d);
88 }
89
Alan Viverette4f64c042014-07-21 17:49:13 -070090 mButtonTintMode = Drawable.parseTintMode(a.getInt(
91 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
92
Alan Viverette91174362014-06-17 14:51:45 -070093 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
Alan Viverettea4264452014-07-28 16:02:55 -070094 mButtonTintList = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
Alan Viverette91174362014-06-17 14:51:45 -070095 mHasButtonTint = true;
96
97 applyButtonTint();
98 }
99
100 final boolean checked = a.getBoolean(
101 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 setChecked(checked);
103
104 a.recycle();
105 }
106
107 public void toggle() {
108 setChecked(!mChecked);
109 }
110
111 @Override
112 public boolean performClick() {
113 /*
114 * XXX: These are tiny, need some surrounding 'expanded touch area',
115 * which will need to be implemented in Button if we only override
116 * performClick()
117 */
118
119 /* When clicked, toggle the state */
120 toggle();
121 return super.performClick();
122 }
123
Steve Zeigler7a367882010-02-23 16:39:08 -0800124 @ViewDebug.ExportedProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public boolean isChecked() {
126 return mChecked;
127 }
128
129 /**
130 * <p>Changes the checked state of this button.</p>
131 *
132 * @param checked true to check the button, false to uncheck it
133 */
134 public void setChecked(boolean checked) {
135 if (mChecked != checked) {
136 mChecked = checked;
137 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700138 notifyViewAccessibilityStateChangedIfNeeded(
139 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141 // Avoid infinite recursions if setChecked() is called from a listener
142 if (mBroadcasting) {
143 return;
144 }
145
146 mBroadcasting = true;
147 if (mOnCheckedChangeListener != null) {
148 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
149 }
150 if (mOnCheckedChangeWidgetListener != null) {
151 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
152 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 mBroadcasting = false;
155 }
156 }
157
158 /**
159 * Register a callback to be invoked when the checked state of this button
160 * changes.
161 *
162 * @param listener the callback to call on checked state change
163 */
164 public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
165 mOnCheckedChangeListener = listener;
166 }
167
168 /**
169 * Register a callback to be invoked when the checked state of this button
170 * changes. This callback is used for internal purpose only.
171 *
172 * @param listener the callback to call on checked state change
173 * @hide
174 */
175 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
176 mOnCheckedChangeWidgetListener = listener;
177 }
178
179 /**
180 * Interface definition for a callback to be invoked when the checked state
181 * of a compound button changed.
182 */
183 public static interface OnCheckedChangeListener {
184 /**
185 * Called when the checked state of a compound button has changed.
186 *
187 * @param buttonView The compound button view whose state has changed.
188 * @param isChecked The new checked state of buttonView.
189 */
190 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
191 }
192
193 /**
Alan Viverette91174362014-06-17 14:51:45 -0700194 * Set the button graphic to a given Drawable, identified by its resource
195 * id.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 *
Alan Viverette91174362014-06-17 14:51:45 -0700197 * @param resid the resource id of the drawable to use as the button
198 * graphic
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
200 public void setButtonDrawable(int resid) {
201 if (resid != 0 && resid == mButtonResource) {
202 return;
203 }
204
205 mButtonResource = resid;
206
207 Drawable d = null;
208 if (mButtonResource != 0) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800209 d = getContext().getDrawable(mButtonResource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211 setButtonDrawable(d);
212 }
213
214 /**
Alan Viverette91174362014-06-17 14:51:45 -0700215 * Set the button graphic to a given Drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 *
Alan Viverette91174362014-06-17 14:51:45 -0700217 * @param d The Drawable to use as the button graphic
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 public void setButtonDrawable(Drawable d) {
Alan Viverette91174362014-06-17 14:51:45 -0700220 if (mButtonDrawable != d) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 if (mButtonDrawable != null) {
222 mButtonDrawable.setCallback(null);
223 unscheduleDrawable(mButtonDrawable);
224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Alan Viverette91174362014-06-17 14:51:45 -0700226 mButtonDrawable = d;
227
228 if (d != null) {
229 d.setCallback(this);
230 d.setLayoutDirection(getLayoutDirection());
231 if (d.isStateful()) {
232 d.setState(getDrawableState());
233 }
234 d.setVisible(getVisibility() == VISIBLE, false);
235 setMinHeight(d.getIntrinsicHeight());
236 applyButtonTint();
237 }
238 }
239 }
240
241 /**
Alan Viverette91174362014-06-17 14:51:45 -0700242 * Applies a tint to the button drawable. Does not modify the current tint
243 * mode, which is {@link PorterDuff.Mode#SRC_ATOP} by default.
244 * <p>
245 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
246 * automatically mutate the drawable and apply the specified tint and tint
247 * mode using
Alan Viverettea4264452014-07-28 16:02:55 -0700248 * {@link Drawable#setTintList(ColorStateList)}.
Alan Viverette91174362014-06-17 14:51:45 -0700249 *
250 * @param tint the tint to apply, may be {@code null} to clear tint
251 *
252 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700253 * @see #setButtonTintList(ColorStateList)
254 * @see Drawable#setTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700255 */
Alan Viverettea4264452014-07-28 16:02:55 -0700256 public void setButtonTintList(@Nullable ColorStateList tint) {
257 mButtonTintList = tint;
Alan Viverette4f64c042014-07-21 17:49:13 -0700258 mHasButtonTint = true;
259
260 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700261 }
262
263 /**
264 * @return the tint applied to the button drawable
265 * @attr ref android.R.styleable#CompoundButton_buttonTint
Alan Viverettea4264452014-07-28 16:02:55 -0700266 * @see #setButtonTintList(ColorStateList)
Alan Viverette91174362014-06-17 14:51:45 -0700267 */
268 @Nullable
Alan Viverettea4264452014-07-28 16:02:55 -0700269 public ColorStateList getButtonTintList() {
270 return mButtonTintList;
Alan Viverette91174362014-06-17 14:51:45 -0700271 }
272
273 /**
274 * Specifies the blending mode used to apply the tint specified by
Alan Viverettea4264452014-07-28 16:02:55 -0700275 * {@link #setButtonTintList(ColorStateList)}} to the button drawable. The
Alan Viverette91174362014-06-17 14:51:45 -0700276 * default mode is {@link PorterDuff.Mode#SRC_ATOP}.
277 *
278 * @param tintMode the blending mode used to apply the tint, may be
279 * {@code null} to clear tint
280 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700281 * @see #getButtonTintMode()
Alan Viverettea4264452014-07-28 16:02:55 -0700282 * @see Drawable#setTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700283 */
284 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
Alan Viverette4f64c042014-07-21 17:49:13 -0700285 mButtonTintMode = tintMode;
286
287 applyButtonTint();
Alan Viverette91174362014-06-17 14:51:45 -0700288 }
289
290 /**
291 * @return the blending mode used to apply the tint to the button drawable
292 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
Alan Viverette4f64c042014-07-21 17:49:13 -0700293 * @see #setButtonTintMode(PorterDuff.Mode)
Alan Viverette91174362014-06-17 14:51:45 -0700294 */
295 @Nullable
296 public PorterDuff.Mode getButtonTintMode() {
297 return mButtonTintMode;
298 }
299
300 private void applyButtonTint() {
301 if (mButtonDrawable != null && mHasButtonTint) {
302 mButtonDrawable = mButtonDrawable.mutate();
Alan Viverettea4264452014-07-28 16:02:55 -0700303 mButtonDrawable.setTintList(mButtonTintList);
304 mButtonDrawable.setTintMode(mButtonTintMode);
Alan Viverette91174362014-06-17 14:51:45 -0700305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307
308 @Override
Svetoslav Ganov30401322011-05-12 18:53:45 -0700309 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
310 super.onInitializeAccessibilityEvent(event);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800311 event.setClassName(CompoundButton.class.getName());
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700312 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700313 }
314
315 @Override
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700316 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
317 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800318 info.setClassName(CompoundButton.class.getName());
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700319 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700320 info.setChecked(mChecked);
321 }
322
323 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700324 public int getCompoundPaddingLeft() {
325 int padding = super.getCompoundPaddingLeft();
326 if (!isLayoutRtl()) {
327 final Drawable buttonDrawable = mButtonDrawable;
328 if (buttonDrawable != null) {
329 padding += buttonDrawable.getIntrinsicWidth();
330 }
331 }
332 return padding;
333 }
334
335 @Override
336 public int getCompoundPaddingRight() {
337 int padding = super.getCompoundPaddingRight();
338 if (isLayoutRtl()) {
339 final Drawable buttonDrawable = mButtonDrawable;
340 if (buttonDrawable != null) {
341 padding += buttonDrawable.getIntrinsicWidth();
342 }
343 }
344 return padding;
345 }
346
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800347 /**
348 * @hide
349 */
350 @Override
351 public int getHorizontalOffsetForDrawables() {
352 final Drawable buttonDrawable = mButtonDrawable;
353 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
354 }
355
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700356 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 final Drawable buttonDrawable = mButtonDrawable;
359 if (buttonDrawable != null) {
360 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700361 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
362 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
Alan Viverette61956602014-04-22 19:07:06 -0700364 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 switch (verticalGravity) {
366 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700367 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 break;
369 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700370 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 break;
Alan Viverette61956602014-04-22 19:07:06 -0700372 default:
373 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
Alan Viverette61956602014-04-22 19:07:06 -0700375 final int bottom = top + drawableHeight;
376 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
377 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700379 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700380
381 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700382 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700383 background.setHotspotBounds(left, top, right, bottom);
384 }
385 }
386
387 super.onDraw(canvas);
388
389 if (buttonDrawable != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 buttonDrawable.draw(canvas);
391 }
392 }
393
394 @Override
395 protected int[] onCreateDrawableState(int extraSpace) {
396 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
397 if (isChecked()) {
398 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
399 }
400 return drawableState;
401 }
402
403 @Override
404 protected void drawableStateChanged() {
405 super.drawableStateChanged();
406
407 if (mButtonDrawable != null) {
408 int[] myDrawableState = getDrawableState();
409
410 // Set the state of the Drawable
411 mButtonDrawable.setState(myDrawableState);
412
413 invalidate();
414 }
415 }
416
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700417 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700418 public void drawableHotspotChanged(float x, float y) {
419 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700420
421 if (mButtonDrawable != null) {
422 mButtonDrawable.setHotspot(x, y);
423 }
424 }
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 @Override
427 protected boolean verifyDrawable(Drawable who) {
428 return super.verifyDrawable(who) || who == mButtonDrawable;
429 }
430
Dianne Hackborne2136772010-11-04 15:08:59 -0700431 @Override
432 public void jumpDrawablesToCurrentState() {
433 super.jumpDrawablesToCurrentState();
434 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
435 }
436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 static class SavedState extends BaseSavedState {
438 boolean checked;
439
440 /**
441 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
442 */
443 SavedState(Parcelable superState) {
444 super(superState);
445 }
446
447 /**
448 * Constructor called from {@link #CREATOR}
449 */
450 private SavedState(Parcel in) {
451 super(in);
452 checked = (Boolean)in.readValue(null);
453 }
454
455 @Override
456 public void writeToParcel(Parcel out, int flags) {
457 super.writeToParcel(out, flags);
458 out.writeValue(checked);
459 }
460
461 @Override
462 public String toString() {
463 return "CompoundButton.SavedState{"
464 + Integer.toHexString(System.identityHashCode(this))
465 + " checked=" + checked + "}";
466 }
467
468 public static final Parcelable.Creator<SavedState> CREATOR
469 = new Parcelable.Creator<SavedState>() {
470 public SavedState createFromParcel(Parcel in) {
471 return new SavedState(in);
472 }
473
474 public SavedState[] newArray(int size) {
475 return new SavedState[size];
476 }
477 };
478 }
479
480 @Override
481 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 Parcelable superState = super.onSaveInstanceState();
483
484 SavedState ss = new SavedState(superState);
485
486 ss.checked = isChecked();
487 return ss;
488 }
489
490 @Override
491 public void onRestoreInstanceState(Parcelable state) {
492 SavedState ss = (SavedState) state;
493
494 super.onRestoreInstanceState(ss.getSuperState());
495 setChecked(ss.checked);
496 requestLayout();
497 }
498}