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 26 /** 27 * Displays checked/unchecked states as a button 28 * with a "light" indicator and by default accompanied with the text "ON" or "OFF". 29 * 30 * @attr ref android.R.styleable#ToggleButton_textOn 31 * @attr ref android.R.styleable#ToggleButton_textOff 32 * @attr ref android.R.styleable#ToggleButton_disabledAlpha 33 */ 34 public class ToggleButton extends CompoundButton { 35 private CharSequence mTextOn; 36 private CharSequence mTextOff; 37 38 private Drawable mIndicatorDrawable; 39 40 private static final int NO_ALPHA = 0xFF; 41 private float mDisabledAlpha; 42 ToggleButton(Context context, AttributeSet attrs, int defStyle)43 public ToggleButton(Context context, AttributeSet attrs, int defStyle) { 44 super(context, attrs, defStyle); 45 46 TypedArray a = 47 context.obtainStyledAttributes( 48 attrs, com.android.internal.R.styleable.ToggleButton, defStyle, 0); 49 mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn); 50 mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff); 51 mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f); 52 syncTextState(); 53 a.recycle(); 54 } 55 ToggleButton(Context context, AttributeSet attrs)56 public ToggleButton(Context context, AttributeSet attrs) { 57 this(context, attrs, com.android.internal.R.attr.buttonStyleToggle); 58 } 59 ToggleButton(Context context)60 public ToggleButton(Context context) { 61 this(context, null); 62 } 63 64 @Override setChecked(boolean checked)65 public void setChecked(boolean checked) { 66 super.setChecked(checked); 67 68 syncTextState(); 69 } 70 syncTextState()71 private void syncTextState() { 72 boolean checked = isChecked(); 73 if (checked && mTextOn != null) { 74 setText(mTextOn); 75 } else if (!checked && mTextOff != null) { 76 setText(mTextOff); 77 } 78 } 79 80 /** 81 * Returns the text for when the button is in the checked state. 82 * 83 * @return The text. 84 */ getTextOn()85 public CharSequence getTextOn() { 86 return mTextOn; 87 } 88 89 /** 90 * Sets the text for when the button is in the checked state. 91 * 92 * @param textOn The text. 93 */ setTextOn(CharSequence textOn)94 public void setTextOn(CharSequence textOn) { 95 mTextOn = textOn; 96 } 97 98 /** 99 * Returns the text for when the button is not in the checked state. 100 * 101 * @return The text. 102 */ getTextOff()103 public CharSequence getTextOff() { 104 return mTextOff; 105 } 106 107 /** 108 * Sets the text for when the button is not in the checked state. 109 * 110 * @param textOff The text. 111 */ setTextOff(CharSequence textOff)112 public void setTextOff(CharSequence textOff) { 113 mTextOff = textOff; 114 } 115 116 @Override onFinishInflate()117 protected void onFinishInflate() { 118 super.onFinishInflate(); 119 120 updateReferenceToIndicatorDrawable(getBackground()); 121 } 122 123 @Override setBackgroundDrawable(Drawable d)124 public void setBackgroundDrawable(Drawable d) { 125 super.setBackgroundDrawable(d); 126 127 updateReferenceToIndicatorDrawable(d); 128 } 129 updateReferenceToIndicatorDrawable(Drawable backgroundDrawable)130 private void updateReferenceToIndicatorDrawable(Drawable backgroundDrawable) { 131 if (backgroundDrawable instanceof LayerDrawable) { 132 LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable; 133 mIndicatorDrawable = 134 layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle); 135 } 136 } 137 138 @Override drawableStateChanged()139 protected void drawableStateChanged() { 140 super.drawableStateChanged(); 141 142 if (mIndicatorDrawable != null) { 143 mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha)); 144 } 145 } 146 147 } 148