Results 1 to 2 of 2
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Rep Power

    Default Why is the defaultValue of my preference not being saved to SharedPreferences?

    In the following SSCCE, the idea is that there is a button in the main activity, and when the user clicks it, (the method showCheckBoxPreferencesValue() is invoked) the default value of the only preference defined in preferences.xml (using android:defaultValue) should be displayed in the textbox below this button.

    In order to get this default value to display in the textbox, I use the method sharedPreferences.getString() and pass Preference does not exist as its second argument, which means if the preference (with the key passed as first argument) does not seem to exist, then the string Preference does not exist is set to the textbox. (Source)

    And that IS happening? What am I doing wrong? See screenshot at the end.


    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android=""
        xmlns:app="" >
        <EditTextPreference android:key="@string/preferences_editTextPreference_key"
            android:defaultValue="@string/preferences_editTextPreference_defaultValue" />

    Java Code:
    public class MainActivity extends Activity {
        private String EDIT_TEXT_PREFERENCE_KEY;
        private TextView textView;
        protected void onCreate(Bundle savedInstanceState) {
            textView = (TextView) findViewById(;
            PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        public void showCheckBoxPreferencesValue(View view) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            EDIT_TEXT_PREFERENCE_KEY = getResources().getString(R.string.preferences_editTextPreference_key);
            String editTextPreferenceValue = sharedPreferences.getString(EDIT_TEXT_PREFERENCE_KEY,
                    "Preference does not exist");
            textView.setText("Checkbox Preference Value: " + editTextPreferenceValue);

    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
        <string name="app_name">Preferences Practice</string>
        <string name="mainActivity_button">Show EditText Preference\'s Value</string>
        <string name="preferences_editTextPreference_key">EditTextPreferenceKey</string>
        <string name="preferences_editTextPreference_title">EditTextPreference Title</string>
        <string name="preferences_editTextPreference_defaultValue">EditTextPreference Default Value String</string>

    Crosspost here
    Last edited by Zarah; 03-10-2016 at 09:47 PM.

  2. #2
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Rep Power

    Default Re: Why isn't the default value of this preference not being read correctly?

    Solved here

Similar Threads

  1. How to read a File correctly?
    By gorgon777 in forum New To Java
    Replies: 8
    Last Post: 02-13-2014, 02:44 PM
  2. Eclipse preference page with SWT.Tree
    By afrey in forum SWT / JFace
    Replies: 2
    Last Post: 02-28-2013, 10:58 PM
  3. Replies: 6
    Last Post: 05-21-2012, 06:33 PM
  4. ImageIO doesn't correctly read some standard JPG Files
    By francojava1 in forum Advanced Java
    Replies: 2
    Last Post: 10-18-2010, 04:33 PM
  5. My Preference Dialog
    By schuetzejanett in forum Eclipse
    Replies: 3
    Last Post: 08-10-2007, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts