blob: b76c2cafd912f106a796b81ffa7a101c8fbe82ba [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.graphics.drawable.LayerDrawable;
23import android.util.AttributeSet;
Ashley Rose55f9f922019-01-28 19:29:36 -050024import android.view.inspector.InspectableProperty;
Svetoslav Ganov76502592011-07-29 10:44:59 -070025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026/**
27 * Displays checked/unchecked states as a button
28 * with a "light" indicator and by default accompanied with the text "ON" or "OFF".
Scott Main41ec6532010-08-19 16:57:07 -070029 *
Scott Main4c359b72012-07-24 15:51:27 -070030 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a>
31 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 *
33 * @attr ref android.R.styleable#ToggleButton_textOn
34 * @attr ref android.R.styleable#ToggleButton_textOff
35 * @attr ref android.R.styleable#ToggleButton_disabledAlpha
36 */
37public class ToggleButton extends CompoundButton {
38 private CharSequence mTextOn;
39 private CharSequence mTextOff;
40
41 private Drawable mIndicatorDrawable;
42
43 private static final int NO_ALPHA = 0xFF;
44 private float mDisabledAlpha;
Alan Viverette617feb92013-09-09 18:09:13 -070045
46 public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
47 super(context, attrs, defStyleAttr, defStyleRes);
48
49 final TypedArray a = context.obtainStyledAttributes(
50 attrs, com.android.internal.R.styleable.ToggleButton, defStyleAttr, defStyleRes);
Aurimas Liutikasab324cf2019-02-07 16:46:38 -080051 saveAttributeDataForStyleable(context, com.android.internal.R.styleable.ToggleButton,
52 attrs, a, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn);
54 mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff);
55 mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f);
56 syncTextState();
57 a.recycle();
58 }
59
Alan Viverette617feb92013-09-09 18:09:13 -070060 public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
61 this(context, attrs, defStyleAttr, 0);
62 }
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 public ToggleButton(Context context, AttributeSet attrs) {
65 this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
66 }
67
68 public ToggleButton(Context context) {
69 this(context, null);
70 }
71
72 @Override
73 public void setChecked(boolean checked) {
74 super.setChecked(checked);
75
76 syncTextState();
77 }
78
79 private void syncTextState() {
80 boolean checked = isChecked();
81 if (checked && mTextOn != null) {
82 setText(mTextOn);
83 } else if (!checked && mTextOff != null) {
84 setText(mTextOff);
85 }
86 }
87
88 /**
89 * Returns the text for when the button is in the checked state.
90 *
91 * @return The text.
92 */
Ashley Rose55f9f922019-01-28 19:29:36 -050093 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public CharSequence getTextOn() {
95 return mTextOn;
96 }
97
98 /**
99 * Sets the text for when the button is in the checked state.
100 *
101 * @param textOn The text.
102 */
103 public void setTextOn(CharSequence textOn) {
104 mTextOn = textOn;
105 }
106
107 /**
108 * Returns the text for when the button is not in the checked state.
109 *
110 * @return The text.
111 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500112 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public CharSequence getTextOff() {
114 return mTextOff;
115 }
116
117 /**
118 * Sets the text for when the button is not in the checked state.
119 *
120 * @param textOff The text.
121 */
122 public void setTextOff(CharSequence textOff) {
123 mTextOff = textOff;
124 }
125
Ashley Rose55f9f922019-01-28 19:29:36 -0500126 /**
127 * Returns the alpha value of the button when it is disabled
128 *
129 * @return the alpha value, 0.0-1.0
130 */
131 @InspectableProperty
132 public float getDisabledAlpha() {
133 return mDisabledAlpha;
134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 @Override
137 protected void onFinishInflate() {
138 super.onFinishInflate();
139
140 updateReferenceToIndicatorDrawable(getBackground());
141 }
142
143 @Override
144 public void setBackgroundDrawable(Drawable d) {
145 super.setBackgroundDrawable(d);
146
147 updateReferenceToIndicatorDrawable(d);
148 }
149
150 private void updateReferenceToIndicatorDrawable(Drawable backgroundDrawable) {
151 if (backgroundDrawable instanceof LayerDrawable) {
152 LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable;
153 mIndicatorDrawable =
154 layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle);
Romain Guyaa1c88d2011-08-26 16:21:03 -0700155 } else {
156 mIndicatorDrawable = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158 }
159
160 @Override
161 protected void drawableStateChanged() {
162 super.drawableStateChanged();
163
164 if (mIndicatorDrawable != null) {
165 mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
166 }
167 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700168
169 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800170 public CharSequence getAccessibilityClassName() {
171 return ToggleButton.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173}