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 
19 import android.annotation.FloatRange;
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;
25 import android.view.inspector.InspectableProperty;
26 
27 /**
28  * Displays checked/unchecked states as a button
29  * with a "light" indicator and by default accompanied with the text "ON" or "OFF".
30  *
31  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a>
32  * guide.</p>
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;
46 
ToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)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);
52         saveAttributeDataForStyleable(context, com.android.internal.R.styleable.ToggleButton,
53                 attrs, a, defStyleAttr, defStyleRes);
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 
ToggleButton(Context context, AttributeSet attrs, int defStyleAttr)61     public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
62         this(context, attrs, defStyleAttr, 0);
63     }
64 
ToggleButton(Context context, AttributeSet attrs)65     public ToggleButton(Context context, AttributeSet attrs) {
66         this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
67     }
68 
ToggleButton(Context context)69     public ToggleButton(Context context) {
70         this(context, null);
71     }
72 
73     @Override
setChecked(boolean checked)74     public void setChecked(boolean checked) {
75         super.setChecked(checked);
76 
77         syncTextState();
78     }
79 
syncTextState()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      */
94     @InspectableProperty
getTextOn()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      */
setTextOn(CharSequence textOn)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      */
113     @InspectableProperty
getTextOff()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      */
setTextOff(CharSequence textOff)123     public void setTextOff(CharSequence textOff) {
124         mTextOff = textOff;
125     }
126 
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
133     @FloatRange(from = 0.0, to = 1.0)
getDisabledAlpha()134     public float getDisabledAlpha() {
135         return mDisabledAlpha;
136     }
137 
138     @Override
onFinishInflate()139     protected void onFinishInflate() {
140         super.onFinishInflate();
141 
142         updateReferenceToIndicatorDrawable(getBackground());
143     }
144 
145     @Override
setBackgroundDrawable(Drawable d)146     public void setBackgroundDrawable(Drawable d) {
147         super.setBackgroundDrawable(d);
148 
149         updateReferenceToIndicatorDrawable(d);
150     }
151 
updateReferenceToIndicatorDrawable(Drawable backgroundDrawable)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);
157         } else {
158             mIndicatorDrawable = null;
159         }
160     }
161 
162     @Override
drawableStateChanged()163     protected void drawableStateChanged() {
164         super.drawableStateChanged();
165 
166         if (mIndicatorDrawable != null) {
167             mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
168         }
169     }
170 
171     @Override
getAccessibilityClassName()172     public CharSequence getAccessibilityClassName() {
173         return ToggleButton.class.getName();
174     }
175 }
176