SwitchPreference.java revision 4d065a04ce42c07569cf20651c949c4da97aa4a9
1be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell/* 2be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Copyright (C) 2010 The Android Open Source Project 3be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 4be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 5be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * you may not use this file except in compliance with the License. 6be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * You may obtain a copy of the License at 7be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 8be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 9be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 10be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Unless required by applicable law or agreed to in writing, software 11be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 12be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * See the License for the specific language governing permissions and 14be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * limitations under the License. 15be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 16be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 17be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellpackage android.preference; 18be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 19be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.content.Context; 20be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.content.res.TypedArray; 21be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.util.AttributeSet; 22be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.view.View; 23be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.widget.Checkable; 24be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.widget.CompoundButton; 25be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.widget.Switch; 26be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 27be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell/** 28be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * A {@link Preference} that provides a two-state toggleable option. 29be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * <p> 30be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This preference will store a boolean into the SharedPreferences. 31be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 32be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_summaryOff 33be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_summaryOn 34be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_switchTextOff 35be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_switchTextOn 36be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_disableDependentsState 37be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 38be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellpublic class SwitchPreference extends TwoStatePreference { 394d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette private final Listener mListener = new Listener(); 404d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette 41be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell // Switch text for on and off states 42be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell private CharSequence mSwitchOn; 43be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell private CharSequence mSwitchOff; 44be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 4594f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell private class Listener implements CompoundButton.OnCheckedChangeListener { 46be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell @Override 47be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 4894f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell if (!callChangeListener(isChecked)) { 4994f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell // Listener didn't like it, change it back. 5094f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell // CompoundButton will make sure we don't recurse. 5194f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell buttonView.setChecked(!isChecked); 5294f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell return; 5394f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell } 5494f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell 55be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell SwitchPreference.this.setChecked(isChecked); 56be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 57be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 58be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 59be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 60be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Construct a new SwitchPreference with the given style options. 61be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 62be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param context The Context that will style this preference 63be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param attrs Style attributes that differ from the default 64617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 65617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 66617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 67617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleRes A resource identifier of a style resource that 68617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * supplies default values for the view, used only if 69617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * defStyleAttr is 0 or can not be found in the theme. Can be 0 70617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * to not look for defaults. 71be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 72617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 73617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 74be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 75be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell TypedArray a = context.obtainStyledAttributes(attrs, 76617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette com.android.internal.R.styleable.SwitchPreference, defStyleAttr, defStyleRes); 77be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn)); 78be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff)); 79be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOn(a.getString( 80be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell com.android.internal.R.styleable.SwitchPreference_switchTextOn)); 81be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOff(a.getString( 82be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell com.android.internal.R.styleable.SwitchPreference_switchTextOff)); 83be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setDisableDependentsState(a.getBoolean( 84be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false)); 85be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell a.recycle(); 86be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 87be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 88be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 89be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Construct a new SwitchPreference with the given style options. 90be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 91be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param context The Context that will style this preference 92be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param attrs Style attributes that differ from the default 93617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 94617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 95617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 96617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette */ 97617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 98617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette this(context, attrs, defStyleAttr, 0); 99617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette } 100617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 101617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette /** 102617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * Construct a new SwitchPreference with the given style options. 103617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * 104617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param context The Context that will style this preference 105617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param attrs Style attributes that differ from the default 106be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 107be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public SwitchPreference(Context context, AttributeSet attrs) { 108be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle); 109be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 110be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 111be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 112be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Construct a new SwitchPreference with default style options. 113be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 114be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param context The Context that will style this preference 115be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 116be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public SwitchPreference(Context context) { 117be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell this(context, null); 118be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 119be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 120be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell @Override 121be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell protected void onBindView(View view) { 122be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell super.onBindView(view); 123be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 124be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell View checkableView = view.findViewById(com.android.internal.R.id.switchWidget); 125be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell if (checkableView != null && checkableView instanceof Checkable) { 1264d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette if (checkableView instanceof Switch) { 1274d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette final Switch switchView = (Switch) checkableView; 1284d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette switchView.setOnCheckedChangeListener(null); 1294d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette } 1304d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette 131be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell ((Checkable) checkableView).setChecked(mChecked); 1327650259a597dd24137420d32acc35efc44db381eSvetoslav Ganov 1337650259a597dd24137420d32acc35efc44db381eSvetoslav Ganov sendAccessibilityEvent(checkableView); 134be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 135be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell if (checkableView instanceof Switch) { 136be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell final Switch switchView = (Switch) checkableView; 137be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell switchView.setTextOn(mSwitchOn); 138be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell switchView.setTextOff(mSwitchOff); 139be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell switchView.setOnCheckedChangeListener(mListener); 140be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 141be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 142be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 143be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell syncSummaryView(view); 144be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 145be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 146be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 147be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the on state. 148be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 149be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 150be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param onText Text to display in the on state 151be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 152be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void setSwitchTextOn(CharSequence onText) { 153be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell mSwitchOn = onText; 154be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell notifyChanged(); 155be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 156be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 157be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 158be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the off state. 159be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 160be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 161be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param offText Text to display in the off state 162be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 163be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void setSwitchTextOff(CharSequence offText) { 164be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell mSwitchOff = offText; 165be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell notifyChanged(); 166be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 167be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 168be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 169be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the on state. 170be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 171be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 172be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param resId The text as a string resource ID 173be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 174be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void setSwitchTextOn(int resId) { 175be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOn(getContext().getString(resId)); 176be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 177be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 178be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 179be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the off state. 180be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 181be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 182be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param resId The text as a string resource ID 183be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 184be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void setSwitchTextOff(int resId) { 185be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOff(getContext().getString(resId)); 186be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 187be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 188be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 189be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @return The text that will be displayed on the switch widget in the on state 190be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 191be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public CharSequence getSwitchTextOn() { 192be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell return mSwitchOn; 193be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 194be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 195be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 196be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @return The text that will be displayed on the switch widget in the off state 197be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 198be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public CharSequence getSwitchTextOff() { 199be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell return mSwitchOff; 200be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 201be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell} 202