I use SharedPreferences for settings in the application.
There is an EditTextPreference in which some default value is set. How to get it now? (assign to variable)

Settings menu file:

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="@string/pref_user_profile" > <EditTextPreference android:title="@string/pref_user_id_summary" android:summary="@string/pref_user_id_description" android:defaultValue="@string/pref_user_id_default_value" android:key="prefUserId"/> </PreferenceCategory> 

In activit, I'm trying to get something like this:

  SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); id_num = sharedPrefs.getString("prefUserId", ""); 

In the second quotes, as I understand it, the default value is set. But this is not the value that is in the field defaultValue . How to get it?

    1 answer 1

    Before contacting the preferences, you need to say activation separately, so that it loads the default values.

     PreferenceManager.setDefaultValues(this, R.xml.pref_design, true); //теперь можно получать дефолтные значения 

    Where the first argument is the Context , i.e. your activation in your case, and instead of R.xml.pref_design you should put the ID your xml file with the settings of the form R.ИМЯ_ПАПКИ_В_RES_VALUES_ГДЕ_ЛЕЖИТ_XML_ФАЙЛ.ИМЯ_XML_ФАЙЛА_СОДЕРЖАЩЕГО_НАСТРОЙКИ

    • It turned out, thanks! - redL1ne
    • one
      @Dev_Vlad, always please) - Yuriy SPb