blob: 9255ccb5cda730e00668527123dbef031eed7e97 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.res.TypedArray;
22import android.graphics.drawable.Drawable;
23import android.graphics.drawable.LayerDrawable;
24import android.util.AttributeSet;
Ashley Rose55f9f922019-01-28 19:29:36 -050025import android.view.inspector.InspectableProperty;
Svetoslav Ganov76502592011-07-29 10:44:59 -070026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027/**
28 * Displays checked/unchecked states as a button
29 * with a "light" indicator and by default accompanied with the text "ON" or "OFF".
Scott Main41ec6532010-08-19 16:57:07 -070030 *
Scott Main4c359b72012-07-24 15:51:27 -070031 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/togglebutton.html">Toggle Buttons</a>
32 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 *
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 */
38public 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;
Alan Viverette617feb92013-09-09 18:09:13 -070046
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);
Aurimas Liutikasab324cf2019-02-07 16:46:38 -080052 saveAttributeDataForStyleable(context, com.android.internal.R.styleable.ToggleButton,
53 attrs, a, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 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
Alan Viverette617feb92013-09-09 18:09:13 -070061 public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
62 this(context, attrs, defStyleAttr, 0);
63 }
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 public ToggleButton(Context context, AttributeSet attrs) {
66 this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
67 }
68
69 public ToggleButton(Context context) {
70 this(context, null);
71 }
72
73 @Override
74 public void setChecked(boolean checked) {
75 super.setChecked(checked);
76
77 syncTextState();
78 }
79
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 */
Ashley Rose55f9f922019-01-28 19:29:36 -050094 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 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 */
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 */
Ashley Rose55f9f922019-01-28 19:29:36 -0500113 @InspectableProperty
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 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 */
123 public void setTextOff(CharSequence textOff) {
124 mTextOff = textOff;
125 }
126
Ashley Rose55f9f922019-01-28 19:29:36 -0500127 /**
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
Ashley Rose433cb1d2019-02-28 18:41:26 -0500133 @FloatRange(from = 0.0, to = 1.0)
Ashley Rose55f9f922019-01-28 19:29:36 -0500134 public float getDisabledAlpha() {
135 return mDisabledAlpha;
136 }
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 @Override
139 protected void onFinishInflate() {
140 super.onFinishInflate();
141
142 updateReferenceToIndicatorDrawable(getBackground());
143 }
144
145 @Override
146 public void setBackgroundDrawable(Drawable d) {
147 super.setBackgroundDrawable(d);
148
149 updateReferenceToIndicatorDrawable(d);
150 }
151
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);
Romain Guyaa1c88d2011-08-26 16:21:03 -0700157 } else {
158 mIndicatorDrawable = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 }
160 }
161
162 @Override
163 protected void drawableStateChanged() {
164 super.drawableStateChanged();
165
166 if (mIndicatorDrawable != null) {
167 mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
168 }
169 }
Svetoslav Ganov76502592011-07-29 10:44:59 -0700170
171 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800172 public CharSequence getAccessibilityClassName() {
173 return ToggleButton.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175}