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 | |
Ashley Rose | 433cb1d | 2019-02-28 18:41:26 -0500 | [diff] [blame] | 19 | import android.annotation.FloatRange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.graphics.drawable.LayerDrawable; |
| 24 | import android.util.AttributeSet; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 25 | import android.view.inspector.InspectableProperty; |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Displays checked/unchecked states as a button |
| 29 | * with a "light" indicator and by default accompanied with the text "ON" or "OFF". |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 30 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 31 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a> |
| 32 | * guide.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | * |
| 34 | * @attr ref android.R.styleable#ToggleButton_textOn |
| 35 | * @attr ref android.R.styleable#ToggleButton_textOff |
| 36 | * @attr ref android.R.styleable#ToggleButton_disabledAlpha |
| 37 | */ |
| 38 | public class ToggleButton extends CompoundButton { |
| 39 | private CharSequence mTextOn; |
| 40 | private CharSequence mTextOff; |
| 41 | |
| 42 | private Drawable mIndicatorDrawable; |
| 43 | |
| 44 | private static final int NO_ALPHA = 0xFF; |
| 45 | private float mDisabledAlpha; |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 46 | |
| 47 | public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 48 | super(context, attrs, defStyleAttr, defStyleRes); |
| 49 | |
| 50 | final TypedArray a = context.obtainStyledAttributes( |
| 51 | attrs, com.android.internal.R.styleable.ToggleButton, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 52 | saveAttributeDataForStyleable(context, com.android.internal.R.styleable.ToggleButton, |
| 53 | attrs, a, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn); |
| 55 | mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff); |
| 56 | mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f); |
| 57 | syncTextState(); |
| 58 | a.recycle(); |
| 59 | } |
| 60 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 61 | public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 62 | this(context, attrs, defStyleAttr, 0); |
| 63 | } |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | public ToggleButton(Context context, AttributeSet attrs) { |
| 66 | this(context, attrs, com.android.internal.R.attr.buttonStyleToggle); |
| 67 | } |
| 68 | |
| 69 | public ToggleButton(Context context) { |
| 70 | this(context, null); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void setChecked(boolean checked) { |
| 75 | super.setChecked(checked); |
| 76 | |
| 77 | syncTextState(); |
| 78 | } |
| 79 | |
| 80 | private void syncTextState() { |
| 81 | boolean checked = isChecked(); |
| 82 | if (checked && mTextOn != null) { |
| 83 | setText(mTextOn); |
| 84 | } else if (!checked && mTextOff != null) { |
| 85 | setText(mTextOff); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the text for when the button is in the checked state. |
| 91 | * |
| 92 | * @return The text. |
| 93 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 94 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | public CharSequence getTextOn() { |
| 96 | return mTextOn; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets the text for when the button is in the checked state. |
| 101 | * |
| 102 | * @param textOn The text. |
| 103 | */ |
| 104 | public void setTextOn(CharSequence textOn) { |
| 105 | mTextOn = textOn; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the text for when the button is not in the checked state. |
| 110 | * |
| 111 | * @return The text. |
| 112 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 113 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | public CharSequence getTextOff() { |
| 115 | return mTextOff; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sets the text for when the button is not in the checked state. |
| 120 | * |
| 121 | * @param textOff The text. |
| 122 | */ |
| 123 | public void setTextOff(CharSequence textOff) { |
| 124 | mTextOff = textOff; |
| 125 | } |
| 126 | |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 127 | /** |
| 128 | * Returns the alpha value of the button when it is disabled |
| 129 | * |
| 130 | * @return the alpha value, 0.0-1.0 |
| 131 | */ |
| 132 | @InspectableProperty |
Ashley Rose | 433cb1d | 2019-02-28 18:41:26 -0500 | [diff] [blame] | 133 | @FloatRange(from = 0.0, to = 1.0) |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 134 | public float getDisabledAlpha() { |
| 135 | return mDisabledAlpha; |
| 136 | } |
| 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | @Override |
| 139 | protected void onFinishInflate() { |
| 140 | super.onFinishInflate(); |
| 141 | |
| 142 | updateReferenceToIndicatorDrawable(getBackground()); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void setBackgroundDrawable(Drawable d) { |
| 147 | super.setBackgroundDrawable(d); |
| 148 | |
| 149 | updateReferenceToIndicatorDrawable(d); |
| 150 | } |
| 151 | |
| 152 | private void updateReferenceToIndicatorDrawable(Drawable backgroundDrawable) { |
| 153 | if (backgroundDrawable instanceof LayerDrawable) { |
| 154 | LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable; |
| 155 | mIndicatorDrawable = |
| 156 | layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle); |
Romain Guy | aa1c88d | 2011-08-26 16:21:03 -0700 | [diff] [blame] | 157 | } else { |
| 158 | mIndicatorDrawable = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void drawableStateChanged() { |
| 164 | super.drawableStateChanged(); |
| 165 | |
| 166 | if (mIndicatorDrawable != null) { |
| 167 | mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha)); |
| 168 | } |
| 169 | } |
Svetoslav Ganov | 7650259 | 2011-07-29 10:44:59 -0700 | [diff] [blame] | 170 | |
| 171 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 172 | public CharSequence getAccessibilityClassName() { |
| 173 | return ToggleButton.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |