• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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.accessibility.AccessibilityEvent;
26 import android.view.accessibility.AccessibilityNodeInfo;
27 
28 import com.android.internal.R;
29 
30 /**
31  * Displays checked/unchecked states as a button
32  * with a "light" indicator and by default accompanied with the text "ON" or "OFF".
33  *
34  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a>
35  * guide.</p>
36  *
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  */
41 public 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;
49 
ToggleButton(Context context, AttributeSet attrs, int defStyle)50     public ToggleButton(Context context, AttributeSet attrs, int defStyle) {
51         super(context, attrs, defStyle);
52 
53         TypedArray a =
54             context.obtainStyledAttributes(
55                     attrs, com.android.internal.R.styleable.ToggleButton, defStyle, 0);
56         mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn);
57         mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff);
58         mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f);
59         syncTextState();
60         a.recycle();
61     }
62 
ToggleButton(Context context, AttributeSet attrs)63     public ToggleButton(Context context, AttributeSet attrs) {
64         this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
65     }
66 
ToggleButton(Context context)67     public ToggleButton(Context context) {
68         this(context, null);
69     }
70 
71     @Override
setChecked(boolean checked)72     public void setChecked(boolean checked) {
73         super.setChecked(checked);
74 
75         syncTextState();
76     }
77 
syncTextState()78     private void syncTextState() {
79         boolean checked = isChecked();
80         if (checked && mTextOn != null) {
81             setText(mTextOn);
82         } else if (!checked && mTextOff != null) {
83             setText(mTextOff);
84         }
85     }
86 
87     /**
88      * Returns the text for when the button is in the checked state.
89      *
90      * @return The text.
91      */
getTextOn()92     public CharSequence getTextOn() {
93         return mTextOn;
94     }
95 
96     /**
97      * Sets the text for when the button is in the checked state.
98      *
99      * @param textOn The text.
100      */
setTextOn(CharSequence textOn)101     public void setTextOn(CharSequence textOn) {
102         mTextOn = textOn;
103     }
104 
105     /**
106      * Returns the text for when the button is not in the checked state.
107      *
108      * @return The text.
109      */
getTextOff()110     public CharSequence getTextOff() {
111         return mTextOff;
112     }
113 
114     /**
115      * Sets the text for when the button is not in the checked state.
116      *
117      * @param textOff The text.
118      */
setTextOff(CharSequence textOff)119     public void setTextOff(CharSequence textOff) {
120         mTextOff = textOff;
121     }
122 
123     @Override
onFinishInflate()124     protected void onFinishInflate() {
125         super.onFinishInflate();
126 
127         updateReferenceToIndicatorDrawable(getBackground());
128     }
129 
130     @Override
setBackgroundDrawable(Drawable d)131     public void setBackgroundDrawable(Drawable d) {
132         super.setBackgroundDrawable(d);
133 
134         updateReferenceToIndicatorDrawable(d);
135     }
136 
updateReferenceToIndicatorDrawable(Drawable backgroundDrawable)137     private void updateReferenceToIndicatorDrawable(Drawable backgroundDrawable) {
138         if (backgroundDrawable instanceof LayerDrawable) {
139             LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable;
140             mIndicatorDrawable =
141                     layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle);
142         } else {
143             mIndicatorDrawable = null;
144         }
145     }
146 
147     @Override
drawableStateChanged()148     protected void drawableStateChanged() {
149         super.drawableStateChanged();
150 
151         if (mIndicatorDrawable != null) {
152             mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
153         }
154     }
155 
156     @Override
onInitializeAccessibilityEvent(AccessibilityEvent event)157     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
158         super.onInitializeAccessibilityEvent(event);
159         event.setClassName(ToggleButton.class.getName());
160     }
161 
162     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)163     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
164         super.onInitializeAccessibilityNodeInfo(info);
165         info.setClassName(ToggleButton.class.getName());
166     }
167 }
168