blob: 8bac6bddc8ec205ba6b319aea7788f14ecc64576 [file] [log] [blame]
Adam Powellbe0a4532010-11-29 17:47:48 -08001/*
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
17package android.preference;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.Checkable;
24import android.widget.CompoundButton;
25import 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 */
38public 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
Adam Powell94f67e22011-08-23 20:35:49 -070044 private class Listener implements CompoundButton.OnCheckedChangeListener {
Adam Powellbe0a4532010-11-29 17:47:48 -080045 @Override
46 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Adam Powell94f67e22011-08-23 20:35:49 -070047 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
Adam Powellbe0a4532010-11-29 17:47:48 -080054 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 */
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 */
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 */
96 public SwitchPreference(Context context) {
97 this(context, null);
98 }
99
100 @Override
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);
Svetoslav Ganov76502592011-07-29 10:44:59 -0700107
108 sendAccessibilityEvent(checkableView);
Adam Powellbe0a4532010-11-29 17:47:48 -0800109
110 if (checkableView instanceof Switch) {
111 final Switch switchView = (Switch) checkableView;
112 switchView.setTextOn(mSwitchOn);
113 switchView.setTextOff(mSwitchOff);
114 switchView.setOnCheckedChangeListener(mListener);
115 }
Adam Powellbe0a4532010-11-29 17:47:48 -0800116 }
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 */
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 */
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 */
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 */
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 */
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 */
173 public CharSequence getSwitchTextOff() {
174 return mSwitchOff;
175 }
176}