Android APIs
public class

PreferenceManager

extends Object
java.lang.Object
   ↳ android.support.v7.preference.PreferenceManager

Class Overview

Used to help create Preference hierarchies from activities or XML.

In most cases, clients should use addPreferencesFromResource(int), or addPreferencesFromResource(int).

Summary

Nested Classes
interface PreferenceManager.OnDisplayPreferenceDialogListener Interface definition for a class that will be called when a Preference requests to display a dialog. 
interface PreferenceManager.OnNavigateToScreenListener Interface definition for a class that will be called when a PreferenceScreen requests navigation. 
interface PreferenceManager.OnPreferenceTreeClickListener Interface definition for a callback to be invoked when a Preference in the hierarchy rooted at this PreferenceScreen is clicked. 
Constants
String KEY_HAS_SET_DEFAULT_VALUES
Public Methods
PreferenceScreen createPreferenceScreen(Context context)
Preference findPreference(CharSequence key)
Finds a Preference based on its key.
Context getContext()
Returns the context.
static SharedPreferences getDefaultSharedPreferences(Context context)
Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
PreferenceManager.OnDisplayPreferenceDialogListener getOnDisplayPreferenceDialogListener()
PreferenceManager.OnNavigateToScreenListener getOnNavigateToScreenListener()
Returns the PreferenceManager.OnNavigateToScreenListener, if one has been set.
PreferenceManager.OnPreferenceTreeClickListener getOnPreferenceTreeClickListener()
PreferenceScreen getPreferenceScreen()
Returns the root of the preference hierarchy managed by this class.
SharedPreferences getSharedPreferences()
Gets a SharedPreferences instance that preferences managed by this will use.
int getSharedPreferencesMode()
Returns the current mode of the SharedPreferences file that preferences managed by this will use.
String getSharedPreferencesName()
Returns the current name of the SharedPreferences file that preferences managed by this will use.
static void setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
Similar to setDefaultValues(Context, int, boolean) but allows the client to provide the filename and mode of the shared preferences file.
static void setDefaultValues(Context context, int resId, boolean readAgain)
Sets the default values from an XML preference file by reading the values defined by each Preference item's android:defaultValue attribute.
void setOnDisplayPreferenceDialogListener(PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener)
void setOnNavigateToScreenListener(PreferenceManager.OnNavigateToScreenListener listener)
Sets the callback to be invoked when a PreferenceScreen in the hierarchy rooted at this PreferenceManager is clicked.
void setOnPreferenceTreeClickListener(PreferenceManager.OnPreferenceTreeClickListener listener)
Sets the callback to be invoked when a Preference in the hierarchy rooted at this PreferenceManager is clicked.
boolean setPreferences(PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy.
void setSharedPreferencesMode(int sharedPreferencesMode)
Sets the mode of the SharedPreferences file that preferences managed by this will use.
void setSharedPreferencesName(String sharedPreferencesName)
Sets the name of the SharedPreferences file that preferences managed by this will use.
void showDialog(Preference preference)
Called when a preference requests that a dialog be shown to complete a user interaction.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String KEY_HAS_SET_DEFAULT_VALUES

Constant Value: "_has_set_default_values"

Public Methods

public PreferenceScreen createPreferenceScreen (Context context)

public Preference findPreference (CharSequence key)

Finds a Preference based on its key.

Parameters
key The key of the preference to retrieve.
Returns

public Context getContext ()

Returns the context.

Returns
  • The context.

public static SharedPreferences getDefaultSharedPreferences (Context context)

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.

Parameters
context The context of the preferences whose values are wanted.
Returns
  • A SharedPreferences instance that can be used to retrieve and listen to values of the preferences.

public PreferenceManager.OnDisplayPreferenceDialogListener getOnDisplayPreferenceDialogListener ()

public PreferenceManager.OnNavigateToScreenListener getOnNavigateToScreenListener ()

Returns the PreferenceManager.OnNavigateToScreenListener, if one has been set.

public PreferenceManager.OnPreferenceTreeClickListener getOnPreferenceTreeClickListener ()

public PreferenceScreen getPreferenceScreen ()

Returns the root of the preference hierarchy managed by this class.

Returns

public SharedPreferences getSharedPreferences ()

Gets a SharedPreferences instance that preferences managed by this will use.

Returns
  • A SharedPreferences instance pointing to the file that contains the values of preferences that are managed by this.

public int getSharedPreferencesMode ()

Returns the current mode of the SharedPreferences file that preferences managed by this will use.

Returns

public String getSharedPreferencesName ()

Returns the current name of the SharedPreferences file that preferences managed by this will use.

Returns

public static void setDefaultValues (Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)

Similar to setDefaultValues(Context, int, boolean) but allows the client to provide the filename and mode of the shared preferences file.

Parameters
context The context of the shared preferences.
sharedPreferencesName A custom name for the shared preferences file.
sharedPreferencesMode The file creation mode for the shared preferences file, such as MODE_PRIVATE or MODE_PRIVATE
resId The resource ID of the preference XML file.
readAgain Whether to re-read the default values. If false, this method will set the default values only if this method has never been called in the past (or if the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.

Note: this will NOT reset preferences back to their default values. For that functionality, use getDefaultSharedPreferences(Context) and clear it followed by a call to this method with this parameter set to true.

public static void setDefaultValues (Context context, int resId, boolean readAgain)

Sets the default values from an XML preference file by reading the values defined by each Preference item's android:defaultValue attribute. This should be called by the application's main activity.

Parameters
context The context of the shared preferences.
resId The resource ID of the preference XML file.
readAgain Whether to re-read the default values. If false, this method sets the default values only if this method has never been called in the past (or if the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.

Note: this will NOT reset preferences back to their default values. For that functionality, use getDefaultSharedPreferences(Context) and clear it followed by a call to this method with this parameter set to true.

public void setOnDisplayPreferenceDialogListener (PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener)

public void setOnNavigateToScreenListener (PreferenceManager.OnNavigateToScreenListener listener)

Sets the callback to be invoked when a PreferenceScreen in the hierarchy rooted at this PreferenceManager is clicked.

Parameters
listener The callback to be invoked.

public void setOnPreferenceTreeClickListener (PreferenceManager.OnPreferenceTreeClickListener listener)

Sets the callback to be invoked when a Preference in the hierarchy rooted at this PreferenceManager is clicked.

Parameters
listener The callback to be invoked.

public boolean setPreferences (PreferenceScreen preferenceScreen)

Sets the root of the preference hierarchy.

Parameters
preferenceScreen The root PreferenceScreen of the preference hierarchy.
Returns

public void setSharedPreferencesMode (int sharedPreferencesMode)

Sets the mode of the SharedPreferences file that preferences managed by this will use.

Parameters
sharedPreferencesMode The mode of the SharedPreferences file.

public void setSharedPreferencesName (String sharedPreferencesName)

Sets the name of the SharedPreferences file that preferences managed by this will use.

Parameters
sharedPreferencesName The name of the SharedPreferences file.

public void showDialog (Preference preference)

Called when a preference requests that a dialog be shown to complete a user interaction.

Parameters
preference The preference requesting the dialog.