coder
coder

Reputation: 1079

how to use one preference activity for multiple instances of the widgets?

I am working on an android home screen app widget.

i am trying to add preferences to the widget. i can put multiple widgets. but i want to configure it with preferences using shared preference created dynamically.

how can i load the preference from the resource file which fetches the value from the sharedpreference and also updates it?

any suggestion?

Upvotes: 0

Views: 639

Answers (2)

VCD
VCD

Reputation: 949

The key to the problem is to somehow use the setting saved by PreferenceActivity as reference and create your own setting. Below is what I used to achieve having multiple setting for multiple instances of app widget with single PreferenceActivity.

private void onExitPreferenceActivity(Context context, int appWidgetId) {
    // Load the user selected settings saved by PreferenceActivity
    final String SETTING_PREFIX = "COLOR";
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String colorSetting = prefs.getString(SETTING_PREFIX, "");
    // Save the setting of the current widget with widget ID as the postfix
    final SharedPreferences.Editor prefEditor = prefs.edit();
    prefEditor.putString(SETTING_PREFIX + String.valueOf(appWidgetId), colorSetting);
    prefEditor.commit();
}

private void onLoadingWidgetSetting(Context context, int appWidgetId) {
    // Load the setting of a particular widget given a widget ID
    final String SETTING_PREFIX = "COLOR";
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String colorSetting = prefs.getString(SETTING_PREFIX + String.valueOf(appWidgetId), "");
    // Use the loaded setting
    // ....
}

Upvotes: 4

sanczez
sanczez

Reputation: 223

maybe try to write your own preference system maybe on somewhere on filesys. just suggesting

Upvotes: 0

Related Questions