0

I'm developing an Android app that needs to display a Settings screen with various switches and other stuff.

The problem is this: the App is a multi-platform project and I already have custom code that I use to save and load my preferences in a cross-platform manner, so I will not be relying on Android's SharedPreferences mechanism.

What I'd like to do is to create a PreferenceActivity programmatically and populate it with my settings, but have the settings values get loaded/saved by my own custom code.

In other words: I'd like to reuse the nice "list with sections and editing controls" that the PreferenceActivity provides, but I'd like to populate it dynamically and implement my own code to set/get the preferences values.

I can't find much documentation on this around, does anyone have any clue on where to start?

Master_T
  • 7,232
  • 11
  • 72
  • 144

2 Answers2

0

It works pretty much like any other setting. Just get a reference to the UI widget, save it when required and set it when required.

    //get reference to setting
    CheckBoxPreference myCheckBox = (CheckBoxPreference) findPreference("myKey");

    // Set the preference
    myCheckBox.setChecked(true);

    //Save setting
    boolean settingToSave=myCheckBox.isChecked();
Kuffs
  • 35,581
  • 10
  • 79
  • 92
0

Solved it by using code similar to this:

https://stackoverflow.com/a/6129451/300741

The only thing missing from there is the implementation of the listeners to monitor for changes, which can be easily set with the method:

Preference.setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener)
Community
  • 1
  • 1
Master_T
  • 7,232
  • 11
  • 72
  • 144