The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
| 20 | import android.graphics.PorterDuff; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.internal.R; |
| 22 | |
| 23 | import android.content.Context; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 24 | import android.content.res.ColorStateList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.res.TypedArray; |
| 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.view.Gravity; |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 32 | import android.view.ViewDebug; |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 33 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 34 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 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 | */ |
| 50 | public abstract class CompoundButton extends Button implements Checkable { |
| 51 | private boolean mChecked; |
| 52 | private int mButtonResource; |
| 53 | private boolean mBroadcasting; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | private Drawable mButtonDrawable; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 56 | private ColorStateList mButtonTint = null; |
| 57 | private PorterDuff.Mode mButtonTintMode = PorterDuff.Mode.SRC_ATOP; |
| 58 | private boolean mHasButtonTint = false; |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | 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 Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 75 | public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 76 | this(context, attrs, defStyleAttr, 0); |
| 77 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 79 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 85 | final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | if (d != null) { |
| 87 | setButtonDrawable(d); |
| 88 | } |
| 89 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 90 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | 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 Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 123 | @ViewDebug.ExportedProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | 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 Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 137 | notifyViewAccessibilityStateChangedIfNeeded( |
| 138 | AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | |
| 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 Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | 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 Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 193 | * Set the button graphic to a given Drawable, identified by its resource |
| 194 | * id. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 196 | * @param resid the resource id of the drawable to use as the button |
| 197 | * graphic |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | */ |
| 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 Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 208 | d = getContext().getDrawable(mButtonResource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } |
| 210 | setButtonDrawable(d); |
| 211 | } |
| 212 | |
| 213 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 214 | * Set the button graphic to a given Drawable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 216 | * @param d The Drawable to use as the button graphic |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | */ |
| 218 | public void setButtonDrawable(Drawable d) { |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 219 | if (mButtonDrawable != d) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | if (mButtonDrawable != null) { |
| 221 | mButtonDrawable.setCallback(null); |
| 222 | unscheduleDrawable(mButtonDrawable); |
| 223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 225 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 325 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 326 | super.onInitializeAccessibilityEvent(event); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 327 | event.setClassName(CompoundButton.class.getName()); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 328 | event.setChecked(mChecked); |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 332 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 333 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 334 | info.setClassName(CompoundButton.class.getName()); |
Svetoslav Ganov | 0f55cc3 | 2011-07-17 10:51:49 -0700 | [diff] [blame] | 335 | info.setCheckable(true); |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 336 | info.setChecked(mChecked); |
| 337 | } |
| 338 | |
| 339 | @Override |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 340 | 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 Meglio | b878ddb | 2012-11-27 17:44:33 -0800 | [diff] [blame] | 363 | /** |
| 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 Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 372 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | protected void onDraw(Canvas canvas) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | final Drawable buttonDrawable = mButtonDrawable; |
| 375 | if (buttonDrawable != null) { |
| 376 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 377 | final int drawableHeight = buttonDrawable.getIntrinsicHeight(); |
| 378 | final int drawableWidth = buttonDrawable.getIntrinsicWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 380 | final int top; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | switch (verticalGravity) { |
| 382 | case Gravity.BOTTOM: |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 383 | top = getHeight() - drawableHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | break; |
| 385 | case Gravity.CENTER_VERTICAL: |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 386 | top = (getHeight() - drawableHeight) / 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | break; |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 388 | default: |
| 389 | top = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 391 | final int bottom = top + drawableHeight; |
| 392 | final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0; |
| 393 | final int right = isLayoutRtl() ? getWidth() : drawableWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 395 | buttonDrawable.setBounds(left, top, right, bottom); |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 396 | |
| 397 | final Drawable background = getBackground(); |
Alan Viverette | c80ad99 | 2014-05-19 15:46:17 -0700 | [diff] [blame] | 398 | if (background != null) { |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 399 | background.setHotspotBounds(left, top, right, bottom); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | super.onDraw(canvas); |
| 404 | |
| 405 | if (buttonDrawable != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | 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 Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 433 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame^] | 434 | public void drawableHotspotChanged(float x, float y) { |
| 435 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 436 | |
| 437 | if (mButtonDrawable != null) { |
| 438 | mButtonDrawable.setHotspot(x, y); |
| 439 | } |
| 440 | } |
| 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | @Override |
| 443 | protected boolean verifyDrawable(Drawable who) { |
| 444 | return super.verifyDrawable(who) || who == mButtonDrawable; |
| 445 | } |
| 446 | |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 447 | @Override |
| 448 | public void jumpDrawablesToCurrentState() { |
| 449 | super.jumpDrawablesToCurrentState(); |
| 450 | if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState(); |
| 451 | } |
| 452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | 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 | } |