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