blob: cedc7772cb21c743be8d5048f60d7180d9746303 [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
19
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.graphics.drawable.Drawable;
23import android.graphics.drawable.LayerDrawable;
24import android.util.AttributeSet;
Svetoslav Ganov76502592011-07-29 10:44:59 -070025import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080026import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov76502592011-07-29 10:44:59 -070027
28import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30/**
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;
49
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
63 public ToggleButton(Context context, AttributeSet attrs) {
64 this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
65 }
66
67 public ToggleButton(Context context) {
68 this(context, null);
69 }
70
71 @Override
72 public void setChecked(boolean checked) {
73 super.setChecked(checked);
74
75 syncTextState();
76 }
77
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 */
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 */
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 */
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 */
119 public void setTextOff(CharSequence textOff) {
120 mTextOff = textOff;
121 }
122
123 @Override
124 protected void onFinishInflate() {
125 super.onFinishInflate();
126
127 updateReferenceToIndicatorDrawable(getBackground());
128 }
129
130 @Override
131 public void setBackgroundDrawable(Drawable d) {
132 super.setBackgroundDrawable(d);
133
134 updateReferenceToIndicatorDrawable(d);
135 }
136
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);
Romain Guyaa1c88d2011-08-26 16:21:03 -0700142 } else {
143 mIndicatorDrawable = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 }
145 }
146
147 @Override
148 protected void drawableStateChanged() {
149 super.drawableStateChanged();
150
151 if (mIndicatorDrawable != null) {
152 mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
153 }
154 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700155
156 @Override
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800157 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
158 super.onInitializeAccessibilityEvent(event);
159 event.setClassName(ToggleButton.class.getName());
160 }
161
162 @Override
163 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
164 super.onInitializeAccessibilityNodeInfo(info);
165 info.setClassName(ToggleButton.class.getName());
166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167}