FileDocCategorySizeDatePackage
PreferenceGroup.javaAPI DocAndroid 5.1 API11158Thu Mar 12 22:22:10 GMT 2015android.preference

PreferenceGroup

public abstract class PreferenceGroup extends Preference implements GenericInflater.Parent
A container for multiple {@link Preference} objects. It is a base class for Preference objects that are parents, such as {@link PreferenceCategory} and {@link PreferenceScreen}.

Developer Guides

For information about building a settings UI with Preferences, read the Settings guide.

attr
ref android.R.styleable#PreferenceGroup_orderingFromXml

Fields Summary
private List
mPreferenceList
The container for child {@link Preference}s. This is sorted based on the ordering, please use {@link #addPreference(Preference)} instead of adding to this directly.
private boolean
mOrderingAsAdded
private int
mCurrentPreferenceOrder
private boolean
mAttachedToActivity
Constructors Summary
public PreferenceGroup(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)


             
        super(context, attrs, defStyleAttr, defStyleRes);

        mPreferenceList = new ArrayList<Preference>();

        final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.PreferenceGroup, defStyleAttr, defStyleRes);
        mOrderingAsAdded = a.getBoolean(com.android.internal.R.styleable.PreferenceGroup_orderingFromXml,
                mOrderingAsAdded);
        a.recycle();
    
public PreferenceGroup(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

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

        this(context, attrs, 0);
    
Methods Summary
public voidaddItemFromInflater(Preference preference)
Called by the inflater to add an item to this group.

        addPreference(preference);
    
public booleanaddPreference(Preference preference)
Adds a {@link Preference} at the correct position based on the preference's order.

param
preference The preference to add.
return
Whether the preference is now in this group.

        if (mPreferenceList.contains(preference)) {
            // Exists
            return true;
        }
        
        if (preference.getOrder() == Preference.DEFAULT_ORDER) {
            if (mOrderingAsAdded) {
                preference.setOrder(mCurrentPreferenceOrder++);
            }

            if (preference instanceof PreferenceGroup) {
                // TODO: fix (method is called tail recursively when inflating,
                // so we won't end up properly passing this flag down to children
                ((PreferenceGroup)preference).setOrderingAsAdded(mOrderingAsAdded);
            }
        }

        int insertionIndex = Collections.binarySearch(mPreferenceList, preference);
        if (insertionIndex < 0) {
            insertionIndex = insertionIndex * -1 - 1;
        }

        if (!onPrepareAddPreference(preference)) {
            return false;
        }

        synchronized(this) {
            mPreferenceList.add(insertionIndex, preference);
        }

        preference.onAttachedToHierarchy(getPreferenceManager());
        
        if (mAttachedToActivity) {
            preference.onAttachedToActivity();
        }
        
        notifyHierarchyChanged();

        return true;
    
protected voiddispatchRestoreInstanceState(android.os.Bundle container)

        super.dispatchRestoreInstanceState(container);

        // Dispatch to all contained preferences
        final int preferenceCount = getPreferenceCount();
        for (int i = 0; i < preferenceCount; i++) {
            getPreference(i).dispatchRestoreInstanceState(container);
        }
    
protected voiddispatchSaveInstanceState(android.os.Bundle container)

        super.dispatchSaveInstanceState(container);

        // Dispatch to all contained preferences
        final int preferenceCount = getPreferenceCount();
        for (int i = 0; i < preferenceCount; i++) {
            getPreference(i).dispatchSaveInstanceState(container);
        }
    
public PreferencefindPreference(java.lang.CharSequence key)
Finds a {@link Preference} based on its key. If two {@link Preference} share the same key (not recommended), the first to appear will be returned (to retrieve the other preference with the same key, call this method on the first preference). If this preference has the key, it will not be returned.

This will recursively search for the preference into children that are also {@link PreferenceGroup PreferenceGroups}.

param
key The key of the preference to retrieve.
return
The {@link Preference} with the key, or null.

        if (TextUtils.equals(getKey(), key)) {
            return this;
        }
        final int preferenceCount = getPreferenceCount();
        for (int i = 0; i < preferenceCount; i++) {
            final Preference preference = getPreference(i);
            final String curKey = preference.getKey();

            if (curKey != null && curKey.equals(key)) {
                return preference;
            }
            
            if (preference instanceof PreferenceGroup) {
                final Preference returnedPreference = ((PreferenceGroup)preference)
                        .findPreference(key);
                if (returnedPreference != null) {
                    return returnedPreference;
                }
            }
        }

        return null;
    
public PreferencegetPreference(int index)
Returns the {@link Preference} at a particular index.

param
index The index of the {@link Preference} to retrieve.
return
The {@link Preference}.

        return mPreferenceList.get(index);
    
public intgetPreferenceCount()
Returns the number of children {@link Preference}s.

return
The number of preference children in this group.

        return mPreferenceList.size();
    
protected booleanisOnSameScreenAsChildren()
Whether this preference group should be shown on the same screen as its contained preferences.

return
True if the contained preferences should be shown on the same screen as this preference.

        return true;
    
public booleanisOrderingAsAdded()
Whether this group is ordering preferences in the order they are added.

return
Whether this group orders based on the order the children are added.
see
#setOrderingAsAdded(boolean)

        return mOrderingAsAdded;
    
public voidnotifyDependencyChange(boolean disableDependents)

        super.notifyDependencyChange(disableDependents);

        // Child preferences have an implicit dependency on their containing
        // group. Dispatch dependency change to all contained preferences.
        final int preferenceCount = getPreferenceCount();
        for (int i = 0; i < preferenceCount; i++) {
            getPreference(i).onParentChanged(this, disableDependents);
        }
    
protected voidonAttachedToActivity()

        super.onAttachedToActivity();

        // Mark as attached so if a preference is later added to this group, we
        // can tell it we are already attached
        mAttachedToActivity = true;
        
        // Dispatch to all contained preferences
        final int preferenceCount = getPreferenceCount();
        for (int i = 0; i < preferenceCount; i++) {
            getPreference(i).onAttachedToActivity();
        }
    
protected booleanonPrepareAddPreference(Preference preference)
Prepares a {@link Preference} to be added to the group.

param
preference The preference to add.
return
Whether to allow adding the preference (true), or not (false).

        preference.onParentChanged(this, shouldDisableDependents());
        return true;
    
protected voidonPrepareForRemoval()

        super.onPrepareForRemoval();
        
        // We won't be attached to the activity anymore
        mAttachedToActivity = false;
    
public voidremoveAll()
Removes all {@link Preference Preferences} from this group.

        synchronized(this) {
            List<Preference> preferenceList = mPreferenceList;
            for (int i = preferenceList.size() - 1; i >= 0; i--) {
                removePreferenceInt(preferenceList.get(0));
            }
        }
        notifyHierarchyChanged();
    
public booleanremovePreference(Preference preference)
Removes a {@link Preference} from this group.

param
preference The preference to remove.
return
Whether the preference was found and removed.

        final boolean returnValue = removePreferenceInt(preference);
        notifyHierarchyChanged();
        return returnValue;
    
private booleanremovePreferenceInt(Preference preference)

        synchronized(this) {
            preference.onPrepareForRemoval();
            return mPreferenceList.remove(preference);
        }
    
public voidsetOrderingAsAdded(boolean orderingAsAdded)
Whether to order the {@link Preference} children of this group as they are added. If this is false, the ordering will follow each Preference order and default to alphabetic for those without an order.

If this is called after preferences are added, they will not be re-ordered in the order they were added, hence call this method early on.

param
orderingAsAdded Whether to order according to the order added.
see
Preference#setOrder(int)

        mOrderingAsAdded = orderingAsAdded;
    
voidsortPreferences()

        synchronized (this) {
            Collections.sort(mPreferenceList);
        }