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