• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.preference;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.Checkable;
24 import android.widget.CompoundButton;
25 import android.widget.Switch;
26 
27 /**
28  * A {@link Preference} that provides a two-state toggleable option.
29  * <p>
30  * This preference will store a boolean into the SharedPreferences.
31  *
32  * @attr ref android.R.styleable#SwitchPreference_summaryOff
33  * @attr ref android.R.styleable#SwitchPreference_summaryOn
34  * @attr ref android.R.styleable#SwitchPreference_switchTextOff
35  * @attr ref android.R.styleable#SwitchPreference_switchTextOn
36  * @attr ref android.R.styleable#SwitchPreference_disableDependentsState
37  */
38 public class SwitchPreference extends TwoStatePreference {
39     // Switch text for on and off states
40     private CharSequence mSwitchOn;
41     private CharSequence mSwitchOff;
42     private final Listener mListener = new Listener();
43 
44     private class Listener implements CompoundButton.OnCheckedChangeListener {
45         @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)46         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
47             if (!callChangeListener(isChecked)) {
48                 // Listener didn't like it, change it back.
49                 // CompoundButton will make sure we don't recurse.
50                 buttonView.setChecked(!isChecked);
51                 return;
52             }
53 
54             SwitchPreference.this.setChecked(isChecked);
55         }
56     }
57 
58     /**
59      * Construct a new SwitchPreference with the given style options.
60      *
61      * @param context The Context that will style this preference
62      * @param attrs Style attributes that differ from the default
63      * @param defStyle Theme attribute defining the default style options
64      */
SwitchPreference(Context context, AttributeSet attrs, int defStyle)65     public SwitchPreference(Context context, AttributeSet attrs, int defStyle) {
66         super(context, attrs, defStyle);
67 
68         TypedArray a = context.obtainStyledAttributes(attrs,
69                 com.android.internal.R.styleable.SwitchPreference, defStyle, 0);
70         setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn));
71         setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff));
72         setSwitchTextOn(a.getString(
73                 com.android.internal.R.styleable.SwitchPreference_switchTextOn));
74         setSwitchTextOff(a.getString(
75                 com.android.internal.R.styleable.SwitchPreference_switchTextOff));
76         setDisableDependentsState(a.getBoolean(
77                 com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false));
78         a.recycle();
79     }
80 
81     /**
82      * Construct a new SwitchPreference with the given style options.
83      *
84      * @param context The Context that will style this preference
85      * @param attrs Style attributes that differ from the default
86      */
SwitchPreference(Context context, AttributeSet attrs)87     public SwitchPreference(Context context, AttributeSet attrs) {
88         this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
89     }
90 
91     /**
92      * Construct a new SwitchPreference with default style options.
93      *
94      * @param context The Context that will style this preference
95      */
SwitchPreference(Context context)96     public SwitchPreference(Context context) {
97         this(context, null);
98     }
99 
100     @Override
onBindView(View view)101     protected void onBindView(View view) {
102         super.onBindView(view);
103 
104         View checkableView = view.findViewById(com.android.internal.R.id.switchWidget);
105         if (checkableView != null && checkableView instanceof Checkable) {
106             ((Checkable) checkableView).setChecked(mChecked);
107 
108             sendAccessibilityEvent(checkableView);
109 
110             if (checkableView instanceof Switch) {
111                 final Switch switchView = (Switch) checkableView;
112                 switchView.setTextOn(mSwitchOn);
113                 switchView.setTextOff(mSwitchOff);
114                 switchView.setOnCheckedChangeListener(mListener);
115             }
116         }
117 
118         syncSummaryView(view);
119     }
120 
121     /**
122      * Set the text displayed on the switch widget in the on state.
123      * This should be a very short string; one word if possible.
124      *
125      * @param onText Text to display in the on state
126      */
setSwitchTextOn(CharSequence onText)127     public void setSwitchTextOn(CharSequence onText) {
128         mSwitchOn = onText;
129         notifyChanged();
130     }
131 
132     /**
133      * Set the text displayed on the switch widget in the off state.
134      * This should be a very short string; one word if possible.
135      *
136      * @param offText Text to display in the off state
137      */
setSwitchTextOff(CharSequence offText)138     public void setSwitchTextOff(CharSequence offText) {
139         mSwitchOff = offText;
140         notifyChanged();
141     }
142 
143     /**
144      * Set the text displayed on the switch widget in the on state.
145      * This should be a very short string; one word if possible.
146      *
147      * @param resId The text as a string resource ID
148      */
setSwitchTextOn(int resId)149     public void setSwitchTextOn(int resId) {
150         setSwitchTextOn(getContext().getString(resId));
151     }
152 
153     /**
154      * Set the text displayed on the switch widget in the off state.
155      * This should be a very short string; one word if possible.
156      *
157      * @param resId The text as a string resource ID
158      */
setSwitchTextOff(int resId)159     public void setSwitchTextOff(int resId) {
160         setSwitchTextOff(getContext().getString(resId));
161     }
162 
163     /**
164      * @return The text that will be displayed on the switch widget in the on state
165      */
getSwitchTextOn()166     public CharSequence getSwitchTextOn() {
167         return mSwitchOn;
168     }
169 
170     /**
171      * @return The text that will be displayed on the switch widget in the off state
172      */
getSwitchTextOff()173     public CharSequence getSwitchTextOff() {
174         return mSwitchOff;
175     }
176 }
177