this question is similar to mine:
initialize-preferences-from-xml-in-main-activity
Just use this code in onCreate
method:
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
It will load your preferences from XML, and last parameter (readAgain
) will guarantee that user preferences won’t be overwritten. That means setting the readAgain
argument to false
means this will only set the default values if this method has never been called in the past so you don’t need to worry about overriding the user’s settings each time your Activity is created
Take a look into PreferenceManager.setDefaultValues in Android API for further investigation.