27

How do you get the default value of an Android preference defined in XML? I don't want to repeat the definition of the default value in both the code and the preferences XML.

gobernador
  • 5,659
  • 3
  • 32
  • 51
hpique
  • 119,096
  • 131
  • 338
  • 476

3 Answers3

66

You can define default value in resources (/values/bool.xml):

<resources>
    <bool name="mypreference_default">true</bool>
</resources>

Use the value in the preferences.xml:

<CheckBoxPreference
    android:defaultValue="@bool/mypreference_default"
    android:key="mypreference"
    android:title="@string/mypreference_title" />

Then use in code:

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
Boolean value = context.getResources().getBoolean(R.bool.mypreference_default);
Boolean b = p.getBoolean("mypreference", value);
Si8
  • 9,141
  • 22
  • 109
  • 221
Paweł Nadolski
  • 8,296
  • 2
  • 42
  • 32
  • Nice way of doing this. Thanks for sharing! – pilcrowpipe Apr 09 '13 at 06:00
  • 5
    That's exactly what I was looking for, thanks. One more hint: Define the resources not in strings.xml but in a separate file (e.g. constants.xml) to avoid localization. – schnatterer Jul 30 '13 at 16:12
  • 6
    @schnatterer: moving the strings to a different file wont avoid localization. The `strings.xml` filename is just convention. Any resource can be localized by placing it in the correct `res/` subfolder. Although placing default settings into a separate resource file is probably a good idea. – Alex MDC Sep 02 '13 at 10:56
  • 2
    @AlexMDC You're right. I' was just trying to establish a best practice in order to decrease the risk of "unintended translation". – schnatterer Sep 02 '13 at 16:49
  • You can put them in values/attrs.xml . This way, they won't be translated – Madhur Ahuja May 27 '14 at 16:35
  • 3
    Or you can do: – Steven L Sep 22 '14 at 07:06
  • what is the `Boolean value` is called from Activity, what should I replace `context` with? – Si8 Nov 25 '16 at 15:04
  • @Si8, replace with "this" or just remove - it is not needed when called from Activity. – Paweł Nadolski Nov 25 '16 at 16:27
  • I think this is a make up way to resovle the problem Android API introduced. The root reason for this problem is Android API used two DEFAULT values. – progquester Aug 27 '20 at 02:44
13

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.

pixel
  • 24,905
  • 36
  • 149
  • 251
  • 2
    This still doesn't protect against the possible situation where someone down the line removes a preference from the XML and forgets to update the Java code requesting it. In that situation, the application will still compile, but you will end up with a NullPointerException. – idolize Jul 26 '10 at 20:55
  • Yes, this is true, but then a Functional Test should be written to test if all needed preferences are retrieved from XML. – pixel Jul 26 '10 at 20:59
  • 1
    this actually didn't work at all for me. the default values were not set after calling this method. – moonlightcheese Jun 07 '12 at 20:14
  • @moonlightcheese have you found a solution for your problem? I'm stuck at the same point - I call the method but the values aren't set. – astriffe Nov 14 '12 at 16:29
  • 2
    Note that it seems that you should actually call `PreferenceManager.setDefaultValues` with the last parameter as `true`. In fact, if you have multiple preference files this is crucial to the second file being loaded at all. Call `PreferenceManager.setDefaultValues` on all preferences files in the main activity. – zelanix Feb 11 '14 at 00:35
  • Thanks. I use a combination of Pawel's answer and your `setDefaultValues` method. – Limeth Jan 17 '16 at 17:39
  • Thank you. I needed to access default values before running Preferences Activity, so I moved `PreferenceManager.setDefaultValues(this, R.xml.preference, true);` to `onCreate` of rootactivity (`MainActivity`) – Alex Martian Feb 10 '16 at 15:59
1

Create integer.xml under res/values to store integer constants.

In prefereces.xml reference "@integer/default_brightness"

In code context.getResources().getInteger(R.integer.default_brightness)

Derlin
  • 9,572
  • 2
  • 32
  • 53
eugene
  • 320
  • 3
  • 10