blob: 9ba0fe1a55ba4a8da30c7b866f6e6a5fb6cd3720 [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 Viverette91174362014-06-17 14:51:45 -070056 private ColorStateList mButtonTint = null;
57 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 Viverette91174362014-06-17 14:51:45 -070090 if (a.hasValue(R.styleable.CompoundButton_buttonTint)) {
91 mButtonTint = a.getColorStateList(R.styleable.CompoundButton_buttonTint);
92 mButtonTintMode = Drawable.parseTintMode(a.getInt(
93 R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode);
94 mHasButtonTint = true;
95
96 applyButtonTint();
97 }
98
99 final boolean checked = a.getBoolean(
100 com.android.internal.R.styleable.CompoundButton_checked, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 setChecked(checked);
102
103 a.recycle();
104 }
105
106 public void toggle() {
107 setChecked(!mChecked);
108 }
109
110 @Override
111 public boolean performClick() {
112 /*
113 * XXX: These are tiny, need some surrounding 'expanded touch area',
114 * which will need to be implemented in Button if we only override
115 * performClick()
116 */
117
118 /* When clicked, toggle the state */
119 toggle();
120 return super.performClick();
121 }
122
Steve Zeigler7a367882010-02-23 16:39:08 -0800123 @ViewDebug.ExportedProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public boolean isChecked() {
125 return mChecked;
126 }
127
128 /**
129 * <p>Changes the checked state of this button.</p>
130 *
131 * @param checked true to check the button, false to uncheck it
132 */
133 public void setChecked(boolean checked) {
134 if (mChecked != checked) {
135 mChecked = checked;
136 refreshDrawableState();
Alan Viverette77e9a282013-09-12 17:16:09 -0700137 notifyViewAccessibilityStateChangedIfNeeded(
138 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140 // Avoid infinite recursions if setChecked() is called from a listener
141 if (mBroadcasting) {
142 return;
143 }
144
145 mBroadcasting = true;
146 if (mOnCheckedChangeListener != null) {
147 mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
148 }
149 if (mOnCheckedChangeWidgetListener != null) {
150 mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
151 }
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 mBroadcasting = false;
154 }
155 }
156
157 /**
158 * Register a callback to be invoked when the checked state of this button
159 * changes.
160 *
161 * @param listener the callback to call on checked state change
162 */
163 public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
164 mOnCheckedChangeListener = listener;
165 }
166
167 /**
168 * Register a callback to be invoked when the checked state of this button
169 * changes. This callback is used for internal purpose only.
170 *
171 * @param listener the callback to call on checked state change
172 * @hide
173 */
174 void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
175 mOnCheckedChangeWidgetListener = listener;
176 }
177
178 /**
179 * Interface definition for a callback to be invoked when the checked state
180 * of a compound button changed.
181 */
182 public static interface OnCheckedChangeListener {
183 /**
184 * Called when the checked state of a compound button has changed.
185 *
186 * @param buttonView The compound button view whose state has changed.
187 * @param isChecked The new checked state of buttonView.
188 */
189 void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
190 }
191
192 /**
Alan Viverette91174362014-06-17 14:51:45 -0700193 * Set the button graphic to a given Drawable, identified by its resource
194 * id.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 *
Alan Viverette91174362014-06-17 14:51:45 -0700196 * @param resid the resource id of the drawable to use as the button
197 * graphic
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 */
199 public void setButtonDrawable(int resid) {
200 if (resid != 0 && resid == mButtonResource) {
201 return;
202 }
203
204 mButtonResource = resid;
205
206 Drawable d = null;
207 if (mButtonResource != 0) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800208 d = getContext().getDrawable(mButtonResource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 }
210 setButtonDrawable(d);
211 }
212
213 /**
Alan Viverette91174362014-06-17 14:51:45 -0700214 * Set the button graphic to a given Drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 *
Alan Viverette91174362014-06-17 14:51:45 -0700216 * @param d The Drawable to use as the button graphic
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 */
218 public void setButtonDrawable(Drawable d) {
Alan Viverette91174362014-06-17 14:51:45 -0700219 if (mButtonDrawable != d) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 if (mButtonDrawable != null) {
221 mButtonDrawable.setCallback(null);
222 unscheduleDrawable(mButtonDrawable);
223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
Alan Viverette91174362014-06-17 14:51:45 -0700225 mButtonDrawable = d;
226
227 if (d != null) {
228 d.setCallback(this);
229 d.setLayoutDirection(getLayoutDirection());
230 if (d.isStateful()) {
231 d.setState(getDrawableState());
232 }
233 d.setVisible(getVisibility() == VISIBLE, false);
234 setMinHeight(d.getIntrinsicHeight());
235 applyButtonTint();
236 }
237 }
238 }
239
240 /**
241 * Applies a tint to the button drawable.
242 * <p>
243 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
244 * automatically mutate the drawable and apply the specified tint and tint
245 * mode using
246 * {@link Drawable#setTint(ColorStateList, android.graphics.PorterDuff.Mode)}.
247 *
248 * @param tint the tint to apply, may be {@code null} to clear tint
249 * @param tintMode the blending mode used to apply the tint, may be
250 * {@code null} to clear tint
251 *
252 * @attr ref android.R.styleable#CompoundButton_buttonTint
253 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
254 * @see Drawable#setTint(ColorStateList, android.graphics.PorterDuff.Mode)
255 */
256 private void setButtonTint(@Nullable ColorStateList tint,
257 @Nullable PorterDuff.Mode tintMode) {
258 mButtonTint = tint;
259 mButtonTintMode = tintMode;
260 mHasButtonTint = true;
261
262 applyButtonTint();
263 }
264
265 /**
266 * Applies a tint to the button drawable. Does not modify the current tint
267 * mode, which is {@link PorterDuff.Mode#SRC_ATOP} by default.
268 * <p>
269 * Subsequent calls to {@link #setButtonDrawable(Drawable)} will
270 * automatically mutate the drawable and apply the specified tint and tint
271 * mode using
272 * {@link Drawable#setTint(ColorStateList, android.graphics.PorterDuff.Mode)}.
273 *
274 * @param tint the tint to apply, may be {@code null} to clear tint
275 *
276 * @attr ref android.R.styleable#CompoundButton_buttonTint
277 * @see #setButtonTint(ColorStateList, android.graphics.PorterDuff.Mode)
278 */
279 public void setButtonTint(@Nullable ColorStateList tint) {
280 setButtonTint(tint, mButtonTintMode);
281 }
282
283 /**
284 * @return the tint applied to the button drawable
285 * @attr ref android.R.styleable#CompoundButton_buttonTint
286 * @see #setButtonTint(ColorStateList, PorterDuff.Mode)
287 */
288 @Nullable
289 public ColorStateList getButtonTint() {
290 return mButtonTint;
291 }
292
293 /**
294 * Specifies the blending mode used to apply the tint specified by
295 * {@link #setButtonTint(ColorStateList)}} to the button drawable. The
296 * default mode is {@link PorterDuff.Mode#SRC_ATOP}.
297 *
298 * @param tintMode the blending mode used to apply the tint, may be
299 * {@code null} to clear tint
300 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
301 * @see #setButtonTint(ColorStateList)
302 */
303 public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
304 setButtonTint(mButtonTint, tintMode);
305 }
306
307 /**
308 * @return the blending mode used to apply the tint to the button drawable
309 * @attr ref android.R.styleable#CompoundButton_buttonTintMode
310 * @see #setButtonTint(ColorStateList, PorterDuff.Mode)
311 */
312 @Nullable
313 public PorterDuff.Mode getButtonTintMode() {
314 return mButtonTintMode;
315 }
316
317 private void applyButtonTint() {
318 if (mButtonDrawable != null && mHasButtonTint) {
319 mButtonDrawable = mButtonDrawable.mutate();
320 mButtonDrawable.setTint(mButtonTint, mButtonTintMode);
321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 }
323
324 @Override
Svetoslav Ganov30401322011-05-12 18:53:45 -0700325 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
326 super.onInitializeAccessibilityEvent(event);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800327 event.setClassName(CompoundButton.class.getName());
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700328 event.setChecked(mChecked);
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700329 }
330
331 @Override
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700332 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
333 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800334 info.setClassName(CompoundButton.class.getName());
Svetoslav Ganov0f55cc32011-07-17 10:51:49 -0700335 info.setCheckable(true);
Svetoslav Ganov13774d22011-06-15 15:29:51 -0700336 info.setChecked(mChecked);
337 }
338
339 @Override
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700340 public int getCompoundPaddingLeft() {
341 int padding = super.getCompoundPaddingLeft();
342 if (!isLayoutRtl()) {
343 final Drawable buttonDrawable = mButtonDrawable;
344 if (buttonDrawable != null) {
345 padding += buttonDrawable.getIntrinsicWidth();
346 }
347 }
348 return padding;
349 }
350
351 @Override
352 public int getCompoundPaddingRight() {
353 int padding = super.getCompoundPaddingRight();
354 if (isLayoutRtl()) {
355 final Drawable buttonDrawable = mButtonDrawable;
356 if (buttonDrawable != null) {
357 padding += buttonDrawable.getIntrinsicWidth();
358 }
359 }
360 return padding;
361 }
362
Fabrice Di Megliob878ddb2012-11-27 17:44:33 -0800363 /**
364 * @hide
365 */
366 @Override
367 public int getHorizontalOffsetForDrawables() {
368 final Drawable buttonDrawable = mButtonDrawable;
369 return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
370 }
371
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700372 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 protected void onDraw(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 final Drawable buttonDrawable = mButtonDrawable;
375 if (buttonDrawable != null) {
376 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700377 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
378 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379
Alan Viverette61956602014-04-22 19:07:06 -0700380 final int top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 switch (verticalGravity) {
382 case Gravity.BOTTOM:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700383 top = getHeight() - drawableHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 break;
385 case Gravity.CENTER_VERTICAL:
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700386 top = (getHeight() - drawableHeight) / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 break;
Alan Viverette61956602014-04-22 19:07:06 -0700388 default:
389 top = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
Alan Viverette61956602014-04-22 19:07:06 -0700391 final int bottom = top + drawableHeight;
392 final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
393 final int right = isLayoutRtl() ? getWidth() : drawableWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
Fabrice Di Meglio28426792012-06-05 16:41:18 -0700395 buttonDrawable.setBounds(left, top, right, bottom);
Alan Viverette61956602014-04-22 19:07:06 -0700396
397 final Drawable background = getBackground();
Alan Viverettec80ad992014-05-19 15:46:17 -0700398 if (background != null) {
Alan Viverette61956602014-04-22 19:07:06 -0700399 background.setHotspotBounds(left, top, right, bottom);
400 }
401 }
402
403 super.onDraw(canvas);
404
405 if (buttonDrawable != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 buttonDrawable.draw(canvas);
407 }
408 }
409
410 @Override
411 protected int[] onCreateDrawableState(int extraSpace) {
412 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
413 if (isChecked()) {
414 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
415 }
416 return drawableState;
417 }
418
419 @Override
420 protected void drawableStateChanged() {
421 super.drawableStateChanged();
422
423 if (mButtonDrawable != null) {
424 int[] myDrawableState = getDrawableState();
425
426 // Set the state of the Drawable
427 mButtonDrawable.setState(myDrawableState);
428
429 invalidate();
430 }
431 }
432
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700433 @Override
Alan Viverette8de14942014-06-18 18:05:15 -0700434 public void drawableHotspotChanged(float x, float y) {
435 super.drawableHotspotChanged(x, y);
Alan Viverettecebc6ba2014-06-13 15:52:13 -0700436
437 if (mButtonDrawable != null) {
438 mButtonDrawable.setHotspot(x, y);
439 }
440 }
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 @Override
443 protected boolean verifyDrawable(Drawable who) {
444 return super.verifyDrawable(who) || who == mButtonDrawable;
445 }
446
Dianne Hackborne2136772010-11-04 15:08:59 -0700447 @Override
448 public void jumpDrawablesToCurrentState() {
449 super.jumpDrawablesToCurrentState();
450 if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
451 }
452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 static class SavedState extends BaseSavedState {
454 boolean checked;
455
456 /**
457 * Constructor called from {@link CompoundButton#onSaveInstanceState()}
458 */
459 SavedState(Parcelable superState) {
460 super(superState);
461 }
462
463 /**
464 * Constructor called from {@link #CREATOR}
465 */
466 private SavedState(Parcel in) {
467 super(in);
468 checked = (Boolean)in.readValue(null);
469 }
470
471 @Override
472 public void writeToParcel(Parcel out, int flags) {
473 super.writeToParcel(out, flags);
474 out.writeValue(checked);
475 }
476
477 @Override
478 public String toString() {
479 return "CompoundButton.SavedState{"
480 + Integer.toHexString(System.identityHashCode(this))
481 + " checked=" + checked + "}";
482 }
483
484 public static final Parcelable.Creator<SavedState> CREATOR
485 = new Parcelable.Creator<SavedState>() {
486 public SavedState createFromParcel(Parcel in) {
487 return new SavedState(in);
488 }
489
490 public SavedState[] newArray(int size) {
491 return new SavedState[size];
492 }
493 };
494 }
495
496 @Override
497 public Parcelable onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 Parcelable superState = super.onSaveInstanceState();
499
500 SavedState ss = new SavedState(superState);
501
502 ss.checked = isChecked();
503 return ss;
504 }
505
506 @Override
507 public void onRestoreInstanceState(Parcelable state) {
508 SavedState ss = (SavedState) state;
509
510 super.onRestoreInstanceState(ss.getSuperState());
511 setChecked(ss.checked);
512 requestLayout();
513 }
514}