FileDocCategorySizeDatePackage
SwitchPreference.javaAPI DocAndroid 5.1 API7431Thu Mar 12 22:22:10 GMT 2015android.preference

SwitchPreference

public class SwitchPreference extends TwoStatePreference
A {@link Preference} that provides a two-state toggleable option.

This preference will store a boolean into the SharedPreferences.

attr
ref android.R.styleable#SwitchPreference_summaryOff
attr
ref android.R.styleable#SwitchPreference_summaryOn
attr
ref android.R.styleable#SwitchPreference_switchTextOff
attr
ref android.R.styleable#SwitchPreference_switchTextOn
attr
ref android.R.styleable#SwitchPreference_disableDependentsState

Fields Summary
private final Listener
mListener
private CharSequence
mSwitchOn
private CharSequence
mSwitchOff
Constructors Summary
public SwitchPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
Construct a new SwitchPreference with the given style options.

param
context The Context that will style this preference
param
attrs Style attributes that differ from the default
param
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
param
defStyleRes A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

        super(context, attrs, defStyleAttr, defStyleRes);

        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.SwitchPreference, defStyleAttr, defStyleRes);
        setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn));
        setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff));
        setSwitchTextOn(a.getString(
                com.android.internal.R.styleable.SwitchPreference_switchTextOn));
        setSwitchTextOff(a.getString(
                com.android.internal.R.styleable.SwitchPreference_switchTextOff));
        setDisableDependentsState(a.getBoolean(
                com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false));
        a.recycle();
    
public SwitchPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)
Construct a new SwitchPreference with the given style options.

param
context The Context that will style this preference
param
attrs Style attributes that differ from the default
param
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

        this(context, attrs, defStyleAttr, 0);
    
public SwitchPreference(android.content.Context context, android.util.AttributeSet attrs)
Construct a new SwitchPreference with the given style options.

param
context The Context that will style this preference
param
attrs Style attributes that differ from the default

        this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
    
public SwitchPreference(android.content.Context context)
Construct a new SwitchPreference with default style options.

param
context The Context that will style this preference

        this(context, null);
    
Methods Summary
public java.lang.CharSequencegetSwitchTextOff()

return
The text that will be displayed on the switch widget in the off state

        return mSwitchOff;
    
public java.lang.CharSequencegetSwitchTextOn()

return
The text that will be displayed on the switch widget in the on state

        return mSwitchOn;
    
protected voidonBindView(android.view.View view)

        super.onBindView(view);

        View checkableView = view.findViewById(com.android.internal.R.id.switchWidget);
        if (checkableView != null && checkableView instanceof Checkable) {
            if (checkableView instanceof Switch) {
                final Switch switchView = (Switch) checkableView;
                switchView.setOnCheckedChangeListener(null);
            }

            ((Checkable) checkableView).setChecked(mChecked);

            if (checkableView instanceof Switch) {
                final Switch switchView = (Switch) checkableView;
                switchView.setTextOn(mSwitchOn);
                switchView.setTextOff(mSwitchOff);
                switchView.setOnCheckedChangeListener(mListener);
            }
        }

        syncSummaryView(view);
    
public voidsetSwitchTextOff(java.lang.CharSequence offText)
Set the text displayed on the switch widget in the off state. This should be a very short string; one word if possible.

param
offText Text to display in the off state

        mSwitchOff = offText;
        notifyChanged();
    
public voidsetSwitchTextOff(int resId)
Set the text displayed on the switch widget in the off state. This should be a very short string; one word if possible.

param
resId The text as a string resource ID

        setSwitchTextOff(getContext().getString(resId));
    
public voidsetSwitchTextOn(java.lang.CharSequence onText)
Set the text displayed on the switch widget in the on state. This should be a very short string; one word if possible.

param
onText Text to display in the on state

        mSwitchOn = onText;
        notifyChanged();
    
public voidsetSwitchTextOn(int resId)
Set the text displayed on the switch widget in the on state. This should be a very short string; one word if possible.

param
resId The text as a string resource ID

        setSwitchTextOn(getContext().getString(resId));