Android: Preferences – setting default values from prefs.xml

I’ve been working on the Marakana Bootcamp Yamba app. Since I don’t keep my emulator up all day, it’s really annoying setting the preferences all the time. Therefore I looked into how to set the default value in the prefs.xml.

<EditTextPreference
android:defaultValue=”student”
android:key=”username”
android:summary=”summary”
android:title=”@string/username” />
<EditTextPreference
android:defaultValue=”password”
android:key=”password”
android:password=”true”
android:title=”@string/password” />
<EditTextPreference
android:defaultValue=”http://yamba.marakana.com/api&#8221;
android:key=”serverURL”
android:title=”@string/serverurl” />

Setting it in the XML isn’t enough, in your Activity you need to tell the PreferenceManager to load the default values. This one line of code can be done in the onCreate function:

PreferenceManager.setDefaultValues(this, R.xml.preference, false);
Where the last argument, readAgain, is for "Whether to re-read the default values". you want to set this to FALSE so if a user changes it, you aren't overwriting their setting. You can however set it to TRUE if you're testing whether your default value is correct or not. Like I was having issues with my default value for a URL and setting it to TRUE was a life saver.

 

Advertisements
This entry was posted in Android, Coding and tagged , , , . Bookmark the permalink.

7 Responses to Android: Preferences – setting default values from prefs.xml

  1. Paddy says:

    Thats the exact thing which i was looking for, pls tell me more that where to write that one line code of androidpreference manager…???
    i.e.PreferenceManager.setDefaultValues(this, R.xml.preference, false)

  2. Paddy says:

    PreferenceManager.setDefaultValues(this, R.xml.preference, false)

    in above code do i need to replace “this” by my actual default value..???

    • fangstar says:

      This was awhile ago but you leave that line (PreferenceManager.setDefaultValues(this, R.xml.preference, false)) as is.

      Set default values in prefs.xml

      <EditTextPreference
      android:defaultValue=”student”
      android:key=”username”
      android:summary=”summary”
      android:title=”@string/username” />
      <EditTextPreference
      android:defaultValue=”password”
      android:key=”password”
      android:password=”true”
      android:title=”@string/password” />
      <EditTextPreference
      android:defaultValue=”http://yamba.marakana.com/api”
      android:key=”serverURL”
      android:title=”@string/serverurl” />

  3. Paddy says:

    Yup, i’ve did the exactly what u said above. But still my preferences are not showing up after apk installation…
    what should i do..???
    now, do i need to use that one line code of preferencemanager…???
    If yes, then where should i write it…???

    Fangstar, Thank u for ur previous reply!

  4. Paddy says:

    Hello,
    can you provide me the snapshot of java program by putting that one line code i.ePreferenceManager.setDefaultValues(this, R.xml.preference, false);
    So that i will get the actual idea.

    Thank You!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s