FileDocCategorySizeDatePackage
TwoStatePreference.javaAPI DocAndroid 5.1 API8737Thu Mar 12 22:22:10 GMT 2015android.preference

TwoStatePreference

public abstract class TwoStatePreference extends Preference
Common base class for preferences that have two selectable states, persist a boolean value in SharedPreferences, and may have dependent preferences that are enabled/disabled based on the current state.

Fields Summary
private CharSequence
mSummaryOn
private CharSequence
mSummaryOff
boolean
mChecked
private boolean
mCheckedSet
private boolean
mDisableDependentsState
Constructors Summary
public TwoStatePreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)

        super(context, attrs, defStyleAttr, defStyleRes);
    
public TwoStatePreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        this(context, attrs, defStyleAttr, 0);
    
public TwoStatePreference(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, 0);
    
public TwoStatePreference(android.content.Context context)

        this(context, null);
    
Methods Summary
public booleangetDisableDependentsState()
Returns whether dependents are disabled when this preference is on ({@code true}) or when this preference is off ({@code false}).

return
Whether dependents are disabled when this preference is on ({@code true}) or when this preference is off ({@code false}).

        return mDisableDependentsState;
    
public java.lang.CharSequencegetSummaryOff()
Returns the summary to be shown when unchecked.

return
The summary.

        return mSummaryOff;
    
public java.lang.CharSequencegetSummaryOn()
Returns the summary to be shown when checked.

return
The summary.

        return mSummaryOn;
    
public booleanisChecked()
Returns the checked state.

return
The checked state.

        return mChecked;
    
protected voidonClick()

        super.onClick();

        final boolean newValue = !isChecked();
        if (callChangeListener(newValue)) {
            setChecked(newValue);
        }
    
protected java.lang.ObjectonGetDefaultValue(android.content.res.TypedArray a, int index)

        return a.getBoolean(index, false);
    
protected voidonRestoreInstanceState(android.os.Parcelable state)

        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        setChecked(myState.checked);
    
protected android.os.ParcelableonSaveInstanceState()

        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            // No need to save instance state since it's persistent
            return superState;
        }

        final SavedState myState = new SavedState(superState);
        myState.checked = isChecked();
        return myState;
    
protected voidonSetInitialValue(boolean restoreValue, java.lang.Object defaultValue)

        setChecked(restoreValue ? getPersistedBoolean(mChecked)
                : (Boolean) defaultValue);
    
public voidsetChecked(boolean checked)
Sets the checked state and saves it to the {@link SharedPreferences}.

param
checked The checked state.

        // Always persist/notify the first time; don't assume the field's default of false.
        final boolean changed = mChecked != checked;
        if (changed || !mCheckedSet) {
            mChecked = checked;
            mCheckedSet = true;
            persistBoolean(checked);
            if (changed) {
                notifyDependencyChange(shouldDisableDependents());
                notifyChanged();
            }
        }
    
public voidsetDisableDependentsState(boolean disableDependentsState)
Sets whether dependents are disabled when this preference is on ({@code true}) or when this preference is off ({@code false}).

param
disableDependentsState The preference state that should disable dependents.

        mDisableDependentsState = disableDependentsState;
    
public voidsetSummaryOff(java.lang.CharSequence summary)
Sets the summary to be shown when unchecked.

param
summary The summary to be shown when unchecked.

        mSummaryOff = summary;
        if (!isChecked()) {
            notifyChanged();
        }
    
public voidsetSummaryOff(int summaryResId)

see
#setSummaryOff(CharSequence)
param
summaryResId The summary as a resource.

        setSummaryOff(getContext().getString(summaryResId));
    
public voidsetSummaryOn(int summaryResId)

see
#setSummaryOn(CharSequence)
param
summaryResId The summary as a resource.

        setSummaryOn(getContext().getString(summaryResId));
    
public voidsetSummaryOn(java.lang.CharSequence summary)
Sets the summary to be shown when checked.

param
summary The summary to be shown when checked.

        mSummaryOn = summary;
        if (isChecked()) {
            notifyChanged();
        }
    
public booleanshouldDisableDependents()

        boolean shouldDisable = mDisableDependentsState ? mChecked : !mChecked;
        return shouldDisable || super.shouldDisableDependents();
    
voidsyncSummaryView(android.view.View view)
Sync a summary view contained within view's subhierarchy with the correct summary text.

param
view View where a summary should be located

        // Sync the summary view
        TextView summaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
        if (summaryView != null) {
            boolean useDefaultSummary = true;
            if (mChecked && !TextUtils.isEmpty(mSummaryOn)) {
                summaryView.setText(mSummaryOn);
                useDefaultSummary = false;
            } else if (!mChecked && !TextUtils.isEmpty(mSummaryOff)) {
                summaryView.setText(mSummaryOff);
                useDefaultSummary = false;
            }

            if (useDefaultSummary) {
                final CharSequence summary = getSummary();
                if (!TextUtils.isEmpty(summary)) {
                    summaryView.setText(summary);
                    useDefaultSummary = false;
                }
            }

            int newVisibility = View.GONE;
            if (!useDefaultSummary) {
                // Someone has written to it
                newVisibility = View.VISIBLE;
            }
            if (newVisibility != summaryView.getVisibility()) {
                summaryView.setVisibility(newVisibility);
            }
        }