How do you get the default value of an Android preference defined in XML?
Context: I don't want to repeat the definition of the default value in both the code and the preferences XML.
How do you get the default value of an Android preference defined in XML?
Context: I don't want to repeat the definition of the default value in both the code and the preferences XML.
First you need to define default values in your preference XML file. Then you can populate preferences with default values in your main Activity by calling:
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
When you need to retrieve a some preference just call:
int value = prefs.getInt("key"), null);
Since your preferences are populated you won't get null
value.