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 | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 90 | mButtonTintMode = Drawable.parseTintMode(a.getInt( |
| 91 | R.styleable.CompoundButton_buttonTintMode, -1), mButtonTintMode); |
| 92 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 93 | if (a.hasValue(R.styleable.CompoundButton_buttonTint)) { |
| 94 | mButtonTint = a.getColorStateList(R.styleable.CompoundButton_buttonTint); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 95 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | 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 Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 124 | @ViewDebug.ExportedProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | 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 Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 138 | notifyViewAccessibilityStateChangedIfNeeded( |
| 139 | AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | |
| 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 Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | 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 Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 194 | * Set the button graphic to a given Drawable, identified by its resource |
| 195 | * id. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 197 | * @param resid the resource id of the drawable to use as the button |
| 198 | * graphic |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | */ |
| 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 Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 209 | d = getContext().getDrawable(mButtonResource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | setButtonDrawable(d); |
| 212 | } |
| 213 | |
| 214 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 215 | * Set the button graphic to a given Drawable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 217 | * @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] | 218 | */ |
| 219 | public void setButtonDrawable(Drawable d) { |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 220 | if (mButtonDrawable != d) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | if (mButtonDrawable != null) { |
| 222 | mButtonDrawable.setCallback(null); |
| 223 | unscheduleDrawable(mButtonDrawable); |
| 224 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 226 | 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 Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 242 | * 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 Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 248 | * {@link Drawable#setTint(ColorStateList, PorterDuff.Mode)}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 249 | * |
| 250 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 251 | * |
| 252 | * @attr ref android.R.styleable#CompoundButton_buttonTint |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 253 | * @see #setButtonTint(ColorStateList) |
| 254 | * @see Drawable#setTint(ColorStateList, PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 255 | */ |
| 256 | public void setButtonTint(@Nullable ColorStateList tint) { |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 257 | mButtonTint = tint; |
| 258 | mHasButtonTint = true; |
| 259 | |
| 260 | applyButtonTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return the tint applied to the button drawable |
| 265 | * @attr ref android.R.styleable#CompoundButton_buttonTint |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 266 | * @see #setButtonTint(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 267 | */ |
| 268 | @Nullable |
| 269 | public ColorStateList getButtonTint() { |
| 270 | return mButtonTint; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Specifies the blending mode used to apply the tint specified by |
| 275 | * {@link #setButtonTint(ColorStateList)}} to the button drawable. The |
| 276 | * 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 Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 281 | * @see #getButtonTintMode() |
| 282 | * @see Drawable#setTint(ColorStateList, PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 283 | */ |
| 284 | public void setButtonTintMode(@Nullable PorterDuff.Mode tintMode) { |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 285 | mButtonTintMode = tintMode; |
| 286 | |
| 287 | applyButtonTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 288 | } |
| 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 Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame^] | 293 | * @see #setButtonTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 294 | */ |
| 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(); |
| 303 | mButtonDrawable.setTint(mButtonTint, mButtonTintMode); |
| 304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 308 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 309 | super.onInitializeAccessibilityEvent(event); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 310 | event.setClassName(CompoundButton.class.getName()); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 311 | event.setChecked(mChecked); |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Override |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 315 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 316 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 317 | info.setClassName(CompoundButton.class.getName()); |
Svetoslav Ganov | 0f55cc3 | 2011-07-17 10:51:49 -0700 | [diff] [blame] | 318 | info.setCheckable(true); |
Svetoslav Ganov | 13774d2 | 2011-06-15 15:29:51 -0700 | [diff] [blame] | 319 | info.setChecked(mChecked); |
| 320 | } |
| 321 | |
| 322 | @Override |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 323 | public int getCompoundPaddingLeft() { |
| 324 | int padding = super.getCompoundPaddingLeft(); |
| 325 | if (!isLayoutRtl()) { |
| 326 | final Drawable buttonDrawable = mButtonDrawable; |
| 327 | if (buttonDrawable != null) { |
| 328 | padding += buttonDrawable.getIntrinsicWidth(); |
| 329 | } |
| 330 | } |
| 331 | return padding; |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public int getCompoundPaddingRight() { |
| 336 | int padding = super.getCompoundPaddingRight(); |
| 337 | if (isLayoutRtl()) { |
| 338 | final Drawable buttonDrawable = mButtonDrawable; |
| 339 | if (buttonDrawable != null) { |
| 340 | padding += buttonDrawable.getIntrinsicWidth(); |
| 341 | } |
| 342 | } |
| 343 | return padding; |
| 344 | } |
| 345 | |
Fabrice Di Meglio | b878ddb | 2012-11-27 17:44:33 -0800 | [diff] [blame] | 346 | /** |
| 347 | * @hide |
| 348 | */ |
| 349 | @Override |
| 350 | public int getHorizontalOffsetForDrawables() { |
| 351 | final Drawable buttonDrawable = mButtonDrawable; |
| 352 | return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0; |
| 353 | } |
| 354 | |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 355 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | protected void onDraw(Canvas canvas) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | final Drawable buttonDrawable = mButtonDrawable; |
| 358 | if (buttonDrawable != null) { |
| 359 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 360 | final int drawableHeight = buttonDrawable.getIntrinsicHeight(); |
| 361 | final int drawableWidth = buttonDrawable.getIntrinsicWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 363 | final int top; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | switch (verticalGravity) { |
| 365 | case Gravity.BOTTOM: |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 366 | top = getHeight() - drawableHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | break; |
| 368 | case Gravity.CENTER_VERTICAL: |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 369 | top = (getHeight() - drawableHeight) / 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | break; |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 371 | default: |
| 372 | top = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 374 | final int bottom = top + drawableHeight; |
| 375 | final int left = isLayoutRtl() ? getWidth() - drawableWidth : 0; |
| 376 | final int right = isLayoutRtl() ? getWidth() : drawableWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | |
Fabrice Di Meglio | 2842679 | 2012-06-05 16:41:18 -0700 | [diff] [blame] | 378 | buttonDrawable.setBounds(left, top, right, bottom); |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 379 | |
| 380 | final Drawable background = getBackground(); |
Alan Viverette | c80ad99 | 2014-05-19 15:46:17 -0700 | [diff] [blame] | 381 | if (background != null) { |
Alan Viverette | 6195660 | 2014-04-22 19:07:06 -0700 | [diff] [blame] | 382 | background.setHotspotBounds(left, top, right, bottom); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | super.onDraw(canvas); |
| 387 | |
| 388 | if (buttonDrawable != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | buttonDrawable.draw(canvas); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | protected int[] onCreateDrawableState(int extraSpace) { |
| 395 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 396 | if (isChecked()) { |
| 397 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 398 | } |
| 399 | return drawableState; |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | protected void drawableStateChanged() { |
| 404 | super.drawableStateChanged(); |
| 405 | |
| 406 | if (mButtonDrawable != null) { |
| 407 | int[] myDrawableState = getDrawableState(); |
| 408 | |
| 409 | // Set the state of the Drawable |
| 410 | mButtonDrawable.setState(myDrawableState); |
| 411 | |
| 412 | invalidate(); |
| 413 | } |
| 414 | } |
| 415 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 416 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 417 | public void drawableHotspotChanged(float x, float y) { |
| 418 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 419 | |
| 420 | if (mButtonDrawable != null) { |
| 421 | mButtonDrawable.setHotspot(x, y); |
| 422 | } |
| 423 | } |
| 424 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | @Override |
| 426 | protected boolean verifyDrawable(Drawable who) { |
| 427 | return super.verifyDrawable(who) || who == mButtonDrawable; |
| 428 | } |
| 429 | |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 430 | @Override |
| 431 | public void jumpDrawablesToCurrentState() { |
| 432 | super.jumpDrawablesToCurrentState(); |
| 433 | if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState(); |
| 434 | } |
| 435 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | static class SavedState extends BaseSavedState { |
| 437 | boolean checked; |
| 438 | |
| 439 | /** |
| 440 | * Constructor called from {@link CompoundButton#onSaveInstanceState()} |
| 441 | */ |
| 442 | SavedState(Parcelable superState) { |
| 443 | super(superState); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Constructor called from {@link #CREATOR} |
| 448 | */ |
| 449 | private SavedState(Parcel in) { |
| 450 | super(in); |
| 451 | checked = (Boolean)in.readValue(null); |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void writeToParcel(Parcel out, int flags) { |
| 456 | super.writeToParcel(out, flags); |
| 457 | out.writeValue(checked); |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public String toString() { |
| 462 | return "CompoundButton.SavedState{" |
| 463 | + Integer.toHexString(System.identityHashCode(this)) |
| 464 | + " checked=" + checked + "}"; |
| 465 | } |
| 466 | |
| 467 | public static final Parcelable.Creator<SavedState> CREATOR |
| 468 | = new Parcelable.Creator<SavedState>() { |
| 469 | public SavedState createFromParcel(Parcel in) { |
| 470 | return new SavedState(in); |
| 471 | } |
| 472 | |
| 473 | public SavedState[] newArray(int size) { |
| 474 | return new SavedState[size]; |
| 475 | } |
| 476 | }; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public Parcelable onSaveInstanceState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | Parcelable superState = super.onSaveInstanceState(); |
| 482 | |
| 483 | SavedState ss = new SavedState(superState); |
| 484 | |
| 485 | ss.checked = isChecked(); |
| 486 | return ss; |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void onRestoreInstanceState(Parcelable state) { |
| 491 | SavedState ss = (SavedState) state; |
| 492 | |
| 493 | super.onRestoreInstanceState(ss.getSuperState()); |
| 494 | setChecked(ss.checked); |
| 495 | requestLayout(); |
| 496 | } |
| 497 | } |