blob: 59e0c16cde373304aa090651a154a3d4d95460df [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
Ashley Rose433cb1d2019-02-28 18:41:26 -050019import android.annotation.FloatRange;
yingleiw473fc122019-10-11 15:10:55 -070020import android.annotation.NonNull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.LayerDrawable;
25import android.util.AttributeSet;
Ashley Rose55f9f922019-01-28 19:29:36 -050026import android.view.inspector.InspectableProperty;
Svetoslav Ganov76502592011-07-29 10:44:59 -070027
yingleiw473fc122019-10-11 15:10:55 -070028import com.android.internal.R;
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
31 * Displays checked/unchecked states as a button
32 * with a "light" indicator and by default accompanied with the text "ON" or "OFF".
Scott Main41ec6532010-08-19 16:57:07 -070033 *
Scott Main4c359b72012-07-24 15:51:27 -070034 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a>
35 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 *
37 * @attr ref android.R.styleable#ToggleButton_textOn
38 * @attr ref android.R.styleable#ToggleButton_textOff
39 * @attr ref android.R.styleable#ToggleButton_disabledAlpha
40 */
41public class ToggleButton extends CompoundButton {
42 private CharSequence mTextOn;
43 private CharSequence mTextOff;
44
45 private Drawable mIndicatorDrawable;
46
47 private static final int NO_ALPHA = 0xFF;
48 private float mDisabledAlpha;
Alan Viverette617feb92013-09-09 18:09:13 -070049
50 public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
51 super(context, attrs, defStyleAttr, defStyleRes);
52
53 final TypedArray a = context.obtainStyledAttributes(
54 attrs, com.android.internal.R.styleable.ToggleButton, defStyleAttr, defStyleRes);
Aurimas Liutikasab324cf2019-02-07 16:46:38 -080055 saveAttributeDataForStyleable(context, com.android.internal.R.styleable.ToggleButton,
56 attrs, a, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn);
58 mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff);
59 mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f);
60 syncTextState();
yingleiw6a55e062019-12-16 15:11:26 -080061 // Default state is derived from on/off-text, so state has to be updated when on/off-text
62 // are updated.
63 setDefaultStateDescritption();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 a.recycle();
65 }
66
Alan Viverette617feb92013-09-09 18:09:13 -070067 public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
68 this(context, attrs, defStyleAttr, 0);
69 }
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 public ToggleButton(Context context, AttributeSet attrs) {
72 this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
73 }
74
75 public ToggleButton(Context context) {
76 this(context, null);
77 }
78
79 @Override
80 public void setChecked(boolean checked) {
81 super.setChecked(checked);
82
83 syncTextState();
84 }
85
86 private void syncTextState() {
87 boolean checked = isChecked();
88 if (checked && mTextOn != null) {
89 setText(mTextOn);
90 } else if (!checked && mTextOff != null) {
91 setText(mTextOff);
92 }
93 }
94
95 /**
96 * Returns the text for when the button is in the checked state.
97 *
98 * @return The text.
99 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500100 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public CharSequence getTextOn() {
102 return mTextOn;
103 }
104
105 /**
106 * Sets the text for when the button is in the checked state.
107 *
108 * @param textOn The text.
109 */
110 public void setTextOn(CharSequence textOn) {
111 mTextOn = textOn;
yingleiw473fc122019-10-11 15:10:55 -0700112 // Default state is derived from on/off-text, so state has to be updated when on/off-text
113 // are updated.
114 setDefaultStateDescritption();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116
117 /**
118 * Returns the text for when the button is not in the checked state.
119 *
120 * @return The text.
121 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500122 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public CharSequence getTextOff() {
124 return mTextOff;
125 }
126
127 /**
128 * Sets the text for when the button is not in the checked state.
129 *
130 * @param textOff The text.
131 */
132 public void setTextOff(CharSequence textOff) {
133 mTextOff = textOff;
yingleiw473fc122019-10-11 15:10:55 -0700134 // Default state is derived from on/off-text, so state has to be updated when on/off-text
135 // are updated.
136 setDefaultStateDescritption();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138
Ashley Rose55f9f922019-01-28 19:29:36 -0500139 /**
140 * Returns the alpha value of the button when it is disabled
141 *
142 * @return the alpha value, 0.0-1.0
143 */
144 @InspectableProperty
Ashley Rose433cb1d2019-02-28 18:41:26 -0500145 @FloatRange(from = 0.0, to = 1.0)
Ashley Rose55f9f922019-01-28 19:29:36 -0500146 public float getDisabledAlpha() {
147 return mDisabledAlpha;
148 }
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 @Override
151 protected void onFinishInflate() {
152 super.onFinishInflate();
153
154 updateReferenceToIndicatorDrawable(getBackground());
155 }
156
157 @Override
158 public void setBackgroundDrawable(Drawable d) {
159 super.setBackgroundDrawable(d);
160
161 updateReferenceToIndicatorDrawable(d);
162 }
163
164 private void updateReferenceToIndicatorDrawable(Drawable backgroundDrawable) {
165 if (backgroundDrawable instanceof LayerDrawable) {
166 LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable;
167 mIndicatorDrawable =
168 layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle);
Romain Guyaa1c88d2011-08-26 16:21:03 -0700169 } else {
170 mIndicatorDrawable = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 }
172 }
173
174 @Override
175 protected void drawableStateChanged() {
176 super.drawableStateChanged();
177
178 if (mIndicatorDrawable != null) {
179 mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
180 }
181 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700182
183 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800184 public CharSequence getAccessibilityClassName() {
185 return ToggleButton.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800186 }
yingleiw473fc122019-10-11 15:10:55 -0700187
188 /** @hide **/
189 @Override
190 @NonNull
191 protected CharSequence getButtonStateDescription() {
192 if (isChecked()) {
193 return mTextOn == null ? getResources().getString(R.string.capital_on) : mTextOn;
194 } else {
195 return mTextOff == null ? getResources().getString(R.string.capital_off) : mTextOff;
196 }
197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198}