ef2011
ef2011

Reputation: 10631

How to Initialize Preferences Screen on First Invocation?

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?

Upvotes: 1

Views: 836

Answers (2)

dmon
dmon

Reputation: 30168

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

<CheckBoxPreference ...
   android:defaultValue="true" 
   ... />

Upvotes: 1

BFil
BFil

Reputation: 13096

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

<EditTextPreference android:defaultValue="whatever" />

Upvotes: 1

Related Questions