FileDocCategorySizeDatePackage
CheckBoxPreference.javaAPI DocAndroid 1.5 API9176Wed May 06 22:41:56 BST 2009android.preference

CheckBoxPreference

public class CheckBoxPreference extends Preference
A {@link Preference} that provides checkbox widget functionality.

This preference will store a boolean into the SharedPreferences.

attr
ref android.R.styleable#CheckBoxPreference_summaryOff
attr
ref android.R.styleable#CheckBoxPreference_summaryOn
attr
ref android.R.styleable#CheckBoxPreference_disableDependentsState

Fields Summary
private CharSequence
mSummaryOn
private CharSequence
mSummaryOff
private boolean
mChecked
private boolean
mDisableDependentsState
Constructors Summary
public CheckBoxPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);
        
        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.CheckBoxPreference, defStyle, 0);
        mSummaryOn = a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn);
        mSummaryOff = a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff);
        mDisableDependentsState = a.getBoolean(
                com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState, false);
        a.recycle();
    
public CheckBoxPreference(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
    
public CheckBoxPreference(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 voidonBindView(android.view.View view)

        super.onBindView(view);
        
        View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
        if (checkboxView != null && checkboxView instanceof Checkable) {
            ((Checkable) checkboxView).setChecked(mChecked);
        }

        // Sync the summary view
        TextView summaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
        if (summaryView != null) {
            boolean useDefaultSummary = true;
            if (mChecked && mSummaryOn != null) {
                summaryView.setText(mSummaryOn);
                useDefaultSummary = false;
            } else if (!mChecked && mSummaryOff != null) {
                summaryView.setText(mSummaryOff);
                useDefaultSummary = false;
            }
            
            if (useDefaultSummary) {
                final CharSequence summary = getSummary();
                if (summary != null) {
                    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);
            }
        }
    
protected voidonClick()

        super.onClick();
        
        boolean newValue = !isChecked();
        
        if (!callChangeListener(newValue)) {
            return;
        }
        
        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.

        mChecked = checked;

        persistBoolean(checked);
     
        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();