FileDocCategorySizeDatePackage
MyPreference.javaAPI DocAndroid 1.5 API5486Wed May 06 22:41:08 BST 2009com.example.android.apis.app

MyPreference

public class MyPreference extends android.preference.Preference
This is an example of a custom preference type. The preference counts the number of clicks it has received and stores/retrieves it from the storage.

Fields Summary
private int
mClickCounter
Constructors Summary
public MyPreference(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
        
        setWidgetLayoutResource(R.layout.preference_widget_mypreference);        
    
Methods Summary
protected voidonBindView(android.view.View view)

        super.onBindView(view);
        
        // Set our custom views inside the layout
        final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
        if (myTextView != null) {
            myTextView.setText(String.valueOf(mClickCounter));
        }
    
protected voidonClick()

        int newValue = mClickCounter + 1;
        // Give the client a chance to ignore this change if they deem it
        // invalid
        if (!callChangeListener(newValue)) {
            // They don't want the value to be set
            return;
        }
        
        // Increment counter
        mClickCounter = newValue;
        
        // Save to persistent storage (this method will make sure this
        // preference should be persistent, along with other useful checks)
        persistInt(mClickCounter);
        
        // Data has changed, notify so UI can be refreshed!
        notifyChanged();
    
protected java.lang.ObjectonGetDefaultValue(android.content.res.TypedArray a, int index)

        // This preference type's value type is Integer, so we read the default
        // value from the attributes as an Integer.
        return a.getInteger(index, 0);
    
protected voidonRestoreInstanceState(android.os.Parcelable state)

        if (!state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }
     
        // Restore the instance state
        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mClickCounter = myState.clickCounter;
        notifyChanged();
    
protected android.os.ParcelableonSaveInstanceState()

        /*
         * Suppose a client uses this preference type without persisting. We
         * must save the instance state so it is able to, for example, survive
         * orientation changes.
         */
        
        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            // No need to save instance state since it's persistent
            return superState;
        }

        // Save the instance state
        final SavedState myState = new SavedState(superState);
        myState.clickCounter = mClickCounter;
        return myState;
    
protected voidonSetInitialValue(boolean restoreValue, java.lang.Object defaultValue)

        if (restoreValue) {
            // Restore state
            mClickCounter = getPersistedInt(mClickCounter);
        } else {
            // Set state
            int value = (Integer) defaultValue;
            mClickCounter = value;
            persistInt(value);
        }