1

I believe I am correctly initializing preferences from XML. My Preferences Screen also works properly and reflects the correct user selected settings.

However, upon first invocation of that Preferences Screen, none of the settings are checked (checkbox) or selected (list). This, of course, confuses the user as it doesn't reflect the current (default/initial) value.

Since all I do to invoke the Preferences Screen is

startActivity(new Intent(this, EditPreferences.class));

And my EditPreferences class only contains:

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.usersettings);
  }

I am not sure where or how to tell it to pre-initialize the visual display with the default setting.

I have this hunch that all I am missing is a single line somewhere, but I don't know where: XML file? override a method in EditPreferences? Other?

Community
  • 1
  • 1
ef2011
  • 10,431
  • 12
  • 49
  • 67

2 Answers2

1

You can specify a default value on a preference (in your xml layout for example):

<EditTextPreference android:defaultValue="whatever" />
BFil
  • 12,966
  • 3
  • 44
  • 48
  • Thanks + 1. Your answer is identical to @dmon's. I still have a slight problem with ``. What am I missing? – ef2011 May 20 '11 at 14:08
  • 1
    It should work the same way, if you have an array of strings specified in your "entry values", you should put the value of one of those string inside the default value field – BFil May 20 '11 at 14:12
  • That's exactly what I did but it didn't show up on the list (i.e. the list is initially displayed with all "radio buttons" unselected). I then uninstalled the app completely, then re-installed it and now it works. Weird. Thanks again and +1 again. – ef2011 May 20 '11 at 14:18
1

Can't you define the default value in the XML itself?

<CheckBoxPreference ...
   android:defaultValue="true" 
   ... />
dmon
  • 30,048
  • 8
  • 87
  • 96
  • Thanks + 1. That's exactly what I was missing. Now it works for the CheckBox, but it doesn't work for `` for some reason (I tried `android:defaultValue="0"`). What is the "trick" for ``? – ef2011 May 20 '11 at 14:07
  • Accepted as you were the first to provide this correct answer. – ef2011 May 20 '11 at 14:18