Initialize preferences from XML in main Activity

Posted by pixel on Stack Overflow See other posts from Stack Overflow or by pixel
Published on 2010-05-20T13:45:26Z Indexed on 2010/05/20 14:10 UTC
Read the original article Hit count: 485

My problem is that when I start application and user didn't open my PreferenceActivity so when I retrieve them don't get any default values defined in my preference.xml file.

preference.xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="applicationPreference" android:title="@string/config"
    >
    <ListPreference
            android:key="pref1"
            android:defaultValue="default"
            android:title="Title"
            android:summary="Summary"
            android:entries="@array/entry_names"
            android:entryValues="@array/entry_values"
            android:dialogTitle="@string/dialog_title"
    />                  
</PreferenceScreen>

Snippet from my main Activity (onCreate method):

    SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);        
    String pref1 = appPreferences.getString("pref1", null);

In result I end up with a null value.

© Stack Overflow or respective owner

Related posts about android-preferences

Related posts about preferences